我有一个类的两个构造函数Transactions,它们在最后一个参数中有所不同,其中第一个构造函数接受一个Label对象,第二个构造函数接受一个Box对象.
public class Transactions {
private String date;
private String kind;
private int employee;
private Label label;
private Box box;
public Transactions(String date, String kind, int employee, Box box) {
this.date = date;
this.kind = kind;
this.employee = employee;
this.box = box;
}
public Transactions(String date, String kind, int employee, Label label) {
this.date = date;
this.kind = kind;
this.employee = employee;
this.label = label;
}
...
}
Run Code Online (Sandbox Code Playgroud)
比方说,我创建类的对象Transactions是tr.我该如何区分它是哪一个?一个与Label …
我试图创建一个简单的类似记事本的程序以在命令提示符下键入。但是不能完全通过使用scanf()函数按“ Enter”键进入下一行。
#include<stdio.h>
void main ()
{
char c;
for(;;){
scanf("%c", &c);
if(c == "\n"){
printf("\n");
}
else{
printf("%c", c);
}
}
}
Run Code Online (Sandbox Code Playgroud)
(我知道scanf()将新的char留在缓冲区中。我已经尽力摆脱了这个问题。比scanf()之后使用了getch()函数,但我无法使其正常工作。我必须了解这个问题使用scanf()函数,因此我想避免使用字符串或其他方法)
请解释下面的行为,为什么第一个语句有效而另一个语句无效并抛出错误.
public class Test{
private String firstName="John";// is Valid
//Below is invalid
private String lastName;
lastname="Doe";
}
Run Code Online (Sandbox Code Playgroud) (预警 - 我知道有类似的问题,但我相信我的问题不同,因为我希望将花色和排名存储在不同的数组中。如果社区有不同的看法,那么我会很乐意删除该问题。)
我正在尝试随机生成一副牌(也称为洗牌)。在函数generateCard中,我生成一个等级和一个花色。所以它本质上生成一张单卡。我还没有包含任何返回,因为我不知道如何返回两个不同的值,特别是因为它们具有不同的数据类型。我真的很想这样做,所以如果答案可以抵制建议更有效或标准的方法的冲动,我将不胜感激。我是一个初学者,了解如何让不起作用的东西发挥作用,这对我很有帮助。
综上所述。我的问题是,如何首先返回两个不同数据类型的项目?其次,分别收集返回并将它们存储在两个不同的数组中(deckSuitArray 和 DeckRankArray)?
这是我的代码:
package texasHoldem;
import java.util.Random;
public class SingleRound{
public static void main(String[] args) {
char[] deckSuitArray = new char[51];
int[] deckRankArray = new int[51];
for(int i = 0; i < 53; i++){
generateCard();
//wish to cycle though arrays storing random cards at different positions
}
}
public static void generateCard(){ //will remove void
Random ran = new Random();
char suit = '0';
int randomRank = ran.nextInt(13)+1;
System.out.println(randomRank);
int randomSuit = ran.nextInt(4)+1;
switch (randomSuit){ …Run Code Online (Sandbox Code Playgroud) 这是一种编程实践问题.
0 1 2 3 4 5
1 2 3 4 5 0
0 1 2 3 0 1
5 0 5 4 1 2
4 5 4 3 2 3
3 2 1 0 5 4
Run Code Online (Sandbox Code Playgroud)
=========================
好吧,我可以使用'printf'16次,但我不想这样做.会有一些模式..但实际上,我无法弄明白.我挣扎了一个星期..!
我想创建正则表达式,允许所有字符和数字组合以及点,但不包括开头的点.所以有效字符串可以是"1.2.3"或"1.b.34d"或其他东西.但我想禁止像"."这样的字符串.或".1.2.3".
我正在尝试
[^.]{0,1}[ a-zA-Z_\\-\\.0-9]*
Run Code Online (Sandbox Code Playgroud)
但这不起作用.谁有更好的主意?
我需要将地图列表转换为CSV对象,如下所示
List<Map<String,Object>> maps = new ArrayList<Map<String,Object>>();
Run Code Online (Sandbox Code Playgroud)
maps对象包含以下格式的值
地图1:
(header 1, value1)
(header 2, value2)
(header 3, value3)
(header 4, value4)
Run Code Online (Sandbox Code Playgroud)
地图2:
(header 1, value5)
(header 2, value6)
(header 3, value7)
(header 4, value8)
Run Code Online (Sandbox Code Playgroud)
我正在寻找下面的CSV
header1, header2,header3, header4
value1, value2,value3,value4
value5,value6,value7,value8
Run Code Online (Sandbox Code Playgroud)
我试图读取地图(键,值)并写入CSV文件,但它写入格式
标题1,value1
header2,value2
标题3,value3
header4,VALUE4
头1,值5
像这样下面是我试过的代码片段
(Map<String, Object> map : maps) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
w.append(entry.getKey()).append(",").append(entry.getValue()??.toString()).append(??"\n"); } }
Run Code Online (Sandbox Code Playgroud) 任务是while仅使用循环打印以下形状.
*
**
***
****
*****
******
*******
********
*********
Run Code Online (Sandbox Code Playgroud)
以下代码是我已经尝试过的,但不幸的是它不起作用:
#include "stdafx.h"//Visual Studio 2015
#include <stdio.h>
#include <stdlib.h>// using for command system("pause") ;
#include <math.h>
int main()
{
int i=0, k=0;
while (i < 10)
{
while (k <= i)
{
printf("*");
k++;
}
printf("\n");
i++;
}
system("pause");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不能自己调试它.有人可以为我调试这个吗?
我对编程很新,我只是找不到我的输出与所需输出不同的原因.这是我的代码:
#include<stdio.h>
float fallingDistance (int t);
int main () {
int t;
float result=0.0;
printf("t <seconds>\td <meters>\n");
for(t=1;t<=10;t++) {
result = fallingDistance (t);
printf("\t%d\t%.2f\n",t,result);
}
}
float fallingDistance (int t) {
const float Grav = 9.8;
float fallD = 0.5*Grav*(t^2);
return fallD;
}
Run Code Online (Sandbox Code Playgroud)
t=1
0.5*9.8*(1^2) = 4.90
0.5*9.8*(3) = 14.70
现在如果t = 1,fallD应该是0.5*9.8*(1 ^ 2)= 4.90,但输出是14.70.知道为什么吗?
鉴于再次发生:
A(n) = A(n-1) + n*log(n).
我如何找到时间复杂度A(n)?
我写了这个简单的程序,它应该计算用户输入的数字的阶乘。程序应该要求用户停止或继续程序以找到新数的阶乘。
由于大多数时候用户不注意 CapsLock,程序应该接受 Y 或 y 作为是的答案。但是每次我运行这个程序时,即使我输入 Y/y ,它也会被终止!
我用谷歌搜索并发现问题可能是由于new line字符被我的字符输入所接受,所以我修改了 scanf 代码从scanf("%c", &choice);toscanf("%c ", &choice);以适应新行字符,但我的程序在接受 Y/y 作为输入后仍然被终止.
这是代码。如果可能,请让我知道处理此类问题的最佳实践和方法以及所需的更正。
#include<stdio.h>
#include"Disablewarning.h" // header file to disable s_secure warning in visual studio contains #pragma warning (disable : 4996)
void main() {
int factorial=1;//Stores the factorial value
int i; //Counter
char choice;//stores user choice to continue or terminte the program
do {//Makes sure the loop isn't terminated until the user decides
do{
printf("Enter the no whose …Run Code Online (Sandbox Code Playgroud) 所以我有一个对象数组,确切地说是500,我已经在我的代码中声明了.我从int i = 0运行for循环到i <500; 并期望所有对象被初始化或构建.我检查了数组[499]的成员数据,因为它返回true,所以它被构造了.但由于一些奇怪的原因,500给了我一个错误,我认为第500个元素没有构建.有人可以向我解释for循环机制,以及它为什么不构造?我看过其他帖子,看到人们做同样的事情,却没有
提出任何错误.我不确定我的代码是什么问题,请帮忙.第一次问题.如果我问的问题太简单,请提前抱歉.
这是我的代码/执行的两个snippits.

这是我的代码,因为链接不起作用:
public class FinalProject {
public static void main(String[] args) {
Sample[] library = new Sample[500];
for(int i = 0; i < library.length; i++)
{
library[i] = new Sample();
}
System.out.println("Availability of index 1: " + library[499].getAvailability());
}
}
Run Code Online (Sandbox Code Playgroud) java ×6
c ×4
algorithm ×2
arrays ×2
constructor ×2
calc ×1
char ×1
csv ×1
dictionary ×1
do-while ×1
for-loop ×1
list ×1
methods ×1
random ×1
recurrence ×1
regex ×1
return ×1
scanf ×1
while-loop ×1