好吧我觉得这可能很难解释,因为我往往是我班上唯一遇到这个问题的人,但基本上我现在正在学习Java编程。我有一个问题,基本上我必须扫描变量的值然后将该变量带入while循环,问题是程序在启动时不会启动while循环。它让我继续输入数字,但这是问题,它保持扫描数字,但它只需要扫描一次,它不会进入循环。 这是程序
import java.util.Scanner;
public class Week05_NelsonPimentel_Assignment {
public static void main(String args[]){
int veraq;
int times = 0;
Scanner input = new Scanner(System.in);
System.out.println("Please enter how many coins you have");
veraq = input.nextInt();
while(veraq>0){
firstmachine(veraq);
howmanytimesplayed(times);
secondmachine(veraq);
howmanytimesplayed(times);
thirdmachine(veraq);
howmanytimesplayed(times);
}
System.out.println("You were able to play this many times before running out of quarters: " +times);
}
static int howmanytimesplayed(int times)
{
times++;
return times;
}
static int firstmachine(int veraq)
{
int times = 0;
if(times == 33){
veraq = veraq + 24;
times = 0;
System.out.println("Congradulations! On machine number one you have won $6.25!! You have this many coins left: " +veraq);
return veraq;
}
else if( times != 33)
{
veraq = veraq - 1;
times++;
return veraq;
}
return 0;
}
static int secondmachine(int veraq)
{
int times = 0;
if(times == 99){
veraq = veraq + 74;
times = 0;
System.out.println("Congradulations! On machine number two you have won $18.75!! You have this many coins left: " +veraq);
return veraq;
}
else if( times != 99)
{
veraq = veraq - 1;
times++;
return veraq;
}
return 0;
}
static int thirdmachine(int veraq)
{
int times = 0;
if(times == 9){
veraq = veraq + 6;
times = 0;**enter code here**
System.out.println("Congradulations! On machine number three you have won $1.75!! You have this many coins left: " +veraq);
return veraq;
}
else if( times != 9)
{
veraq = veraq - 1;
times++;
return veraq;
}
return 0;
}
}
答案 0 :(得分:1)
你的程序不是在不断地扫描数字,程序中的while循环永远不会被终止,即它在无限循环中运行。
' int '是Java中的原始数据类型。当您调用方法firstmachine(veraq);
并使此方法对变量 veraq 执行某些操作时,这些更改将不会反映在 veraq 变量的范围内主要方法。因此,如果你传递 veraq 的正值,它基本上永远不会小于0,你的while循环将永远运行。此外,您的程序是以这样的方式编写的,即不会打印任何内容,从而让您觉得您的程序不断扫描数字。
您应该了解按值调用的内容以及通过引用调用的内容。然后,您应该了解这些概念如何与Java中的方法传递参数相关。祝你好运:)