Java 对创建具有函数的Totoloto的质疑
嗨,伙计们,我的Totoloto计划有问题。我想创建一个函数来显示键的六个数字,但我不能,因为在我的输出中,在结束计数器位置只显示一个数字。我在函数numeDeChaves中使用System.out.println进行调试,并显示所有数字。如果尝试不使用函数及其工作方式,但我希望在函数中获得清晰的代码,并学习更多如何使用函数 这是错误的输出: Quantas chaves假装gerar?六, 作为查韦斯·格拉达斯·萨昂:1ªchave->2ªchave->3ªchave->4ªchave->5ªchave->6ªchave->36Java 对创建具有函数的Totoloto的质疑,java,Java,嗨,伙计们,我的Totoloto计划有问题。我想创建一个函数来显示键的六个数字,但我不能,因为在我的输出中,在结束计数器位置只显示一个数字。我在函数numeDeChaves中使用System.out.println进行调试,并显示所有数字。如果尝试不使用函数及其工作方式,但我希望在函数中获得清晰的代码,并学习更多如何使用函数 这是错误的输出: Quantas chaves假装gerar?六, 作为查韦斯·格拉达斯·萨昂:1ªchave->2ªchave->3ªchave->4ªchave->5ª
publicstaticvoidmain(字符串[]args){
System.out.println(“Totoloto\n”);
System.out.println(“Quantas chaves假装gerar?”);
int numero=Ler.umInt();
int[]numerocaves=新的int[numero];
int[]chave=新int[8];
System.out.println(“”);
System.out.println(“如chaves geradas são:”);
系统输出打印LN(数字波形(数字波形));
}
公共静态整数数字模式(整数[]数字模式){
int contador=1;
int totalChaves=0;
对于(int i=0;i”);
contador++;
对于(int j=0;j
为什么在方法中只生成一次随机数,却将其指定为numero
次?此外,读取带有外部变量名的源代码非常困难。还有一个。这可能更有用。在方法中,chave aleatória生成一个键。Ex 4 5 45 7 34 22和方法NumeroSaves其假定生成的各种键,例如,如果输入3生成3个键Ex:3 34 45 3 23和4 6 34 12 11 2和2 4 6 45 4 4 3,但为什么要将NumeroLeatorio
分配给chave
中chave
的每个元素?在if
语句的两个路径中都有赋值。因为if有一个重复的数字,所以会生成一个新键。因为在这个程序中,keySorry中不能有重复的数字,但这毫无意义。仅当此数字未出现在数组中时,才应分配此数字。此外,您的程序中有大量的循环。我很确定你可以消除其中的大部分!
public static void main(String[] args) {
System.out.println("Totoloto \n");
System.out.println("Quantas chaves pretende gerar?");
int numero = Ler.umInt();
int[] numeroChaves = new int[numero];
int[] chave = new int[8];
System.out.println("");
System.out.println("As chaves geradas são: ");
System.out.println(numeroDeChaves(numeroChaves));
}
public static int numeroDeChaves(int[] numeroChaves){
int contador = 1;
int totalChaves = 0;
for (int i = 0; i < numeroChaves.length; i++) {
System.out.print(contador + "ª chave -> ");
contador++;
for (int j = 0; j < numeroChaves.length; j++) {
numeroChaves[j] = chaveAleatoria(numeroChaves);
totalChaves = numeroChaves[j];
}
System.out.println("");
}
return totalChaves;
}
public static int chaveAleatoria(int chave[]) {
int numeroAleatorio = (int) (Math.random() * 43) + 6;
int ultimoNumero = chave[0] + 1;
int chaveGerada = 0;
int contarChaves = 0;
for (int i = 1; i < chave.length; i++) {
if (chave[i] == chave[i - 1] && ultimoNumero != chave[i - 1]) {
ultimoNumero = chave[i];
chave[i] = numeroAleatorio;
} else {
chave[i] = numeroAleatorio;
chaveGerada = chave[i];
System.out.print("");
}
}
return chaveGerada;
}