Java中的While循环
我试图编写一个程序,当用户输入“y”或“是”时,它将循环运行Java中的While循环,java,loops,while-loop,Java,Loops,While Loop,我试图编写一个程序,当用户输入“y”或“是”时,它将循环运行 System.out.println(“基于Studnet缺席及其对GPA的影响而浪费的学费”); System.out.println(“输入要考虑的学生人数:”); 学生=键盘.nextInt(); while(选项==y) { 如果(学生>=1&&students这里有很多错误 y是什么?您编写代码时就好像有一个变量y。while(choice==y)行上的y需要用单引号或双引号括起来,具体取决于choice的类型 第一次通过w
System.out.println(“基于Studnet缺席及其对GPA的影响而浪费的学费”);
System.out.println(“输入要考虑的学生人数:”);
学生=键盘.nextInt();
while(选项==y)
{
如果(学生>=1&&students这里有很多错误
y是什么?您编写代码时就好像有一个变量y。while(choice==y)
行上的y需要用单引号或双引号括起来,具体取决于choice
的类型
choice
是什么?它在哪里声明?它的类型是什么nextInt()
)的返回类型是什么?提示:int:-)将影响是否将y用单引号或双引号括起来。查看Java中的char和String,以及它们的比较方式在使用
while
循环时,应首先在声明中声明它的初始值
例如:
String choice=“y”
//等等……
while(choice.equals(“y”){
//你的东西在这里
}
PS
您的“y”
的数据类型是什么?我假设您将其声明为整数
,因为您使用了“==”
符号,您不能在字符串
上使用“双等于”
符号。您应该将其更改为字符串
,并改用“.equals”
试着考虑<代码> do,同时<代码>循环
例://您的声明..等等
字符串选择;
System.out.println(“基于Studnet缺课及其对GPA的影响而浪费的学费”);
System.out.println(“输入要考虑的学生人数:”);
students=keyboard.nextInt();
do{
if(学生>=1&&学生们没有足够的信息来帮助:什么是choice
?什么是y
?-看起来choice
是一个int,但你希望它是一个char?同时学生ID是一个double是奇怪的请告诉我们错误。可能更多的代码会更好。你真的编译了吗?你给出的代码片段,ev嗯,如果我们假设你填写了缺失的变量,就会有很多问题。
System.out.println("Tuition Wasted Based on Studnet Absences and its effacton GPA");
System.out.println("Enter the number of students to consider: ");
students = keyboard.nextInt();
while (choice == y)
{
if (students >= 1 && students <= 5)
{
for(int i = 0; i < students; i++)
{
System.out.print("\nEnter the student ID for student: ");
studentID = keyboard.nextDouble();
System.out.println("Do you want to run the program again?");
choice = keyboard.nextInt();
}
System.out.print("\nEnter the student ID for student: ");
studentID = keyboard.nextDouble();`
System.out.println("Do you want to run the program again?");`
choice = keyboard.next();