Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的While循环_Java_Loops_While Loop - Fatal编程技术网

Java中的While循环

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

我试图编写一个程序,当用户输入“y”或“是”时,它将循环运行

System.out.println(“基于Studnet缺席及其对GPA的影响而浪费的学费”);
System.out.println(“输入要考虑的学生人数:”);
学生=键盘.nextInt();
while(选项==y)
{

如果(学生>=1&&students这里有很多错误

  • y是什么?您编写代码时就好像有一个变量y。while(choice==y)
  • 行上的y需要用单引号或双引号括起来,具体取决于
    choice
    的类型

  • 第一次通过while循环时的
    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();