如何使用Scanner类型的操作数循环while循环?

时间:2019-10-08 11:26:37

标签: java

    package com.company;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        while(sc != 0)
        {
            System.out.println("first number: ");
            int firstNum = sc.nextInt();

            System.out.println("second number: ");
            int secondNum = sc.nextInt();

            System.out.println("The  sum of your numbers: " + (firstNum + secondNum));
        }
    }
}

因此,我的预期目标是拥有一个脚本,该脚本将允许我添加两个整数(由用户使用扫描仪输入选择),一旦添加了这两个整数,我就可以开始一个新的总和。当用户输入0时,我也想打破while循环。

我认为我的错误是我无法在Scanner类型上使用!=运算符有人可以解释我的代码中的缺陷吗? (我已经习惯了python,这可能就是为什么我会犯这个错误)

6 个答案:

答案 0 :(得分:1)

您需要声明超出while范围的变量,并对其进行更新,直到不满足条件为止

尝试一下:

package com.company;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        int firstNum = 1;
        int secondNum = 1;

        while(firstNum !=0 && secondNum != 0)
        {
            System.out.println("first number: ");
            firstNum = sc.nextInt();

            System.out.println("second number: ");
            secondNum = sc.nextInt();

            System.out.println("The  sum of your numbers: " + (firstNum + secondNum));
        }
    }
}

答案 1 :(得分:1)

您应该具有某种“无限”循环,如下所示:

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        while(true)
        {
            System.out.println("first number: ");
            int firstNum = sc.nextInt();
            if (firstNum == 0) {
              break;
            }

            System.out.println("second number: ");
            int secondNum = sc.nextInt();
            if (secondNum == 0) {
              break;
            }

            System.out.println("The  sum of your numbers: " + (firstNum + secondNum));
        }
    }
}

答案 2 :(得分:0)

您好,只需删除while块,因为它没有使用的意义

这里是更正的代码

 package com.company;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

            System.out.println("first number: ");
            int firstNum = sc.nextInt();

            System.out.println("second number: ");
            int secondNum = sc.nextInt();

            System.out.println("The  sum of your numbers: " + (firstNum + secondNum));

    }
}

答案 3 :(得分:0)

您不能将Object sc与整数值0进行比较。您可以执行以下代码。

public static void main(String[] args)
    {
         try (Scanner sc = new Scanner(System.in)) {
             System.out.println("first number: ");
             int firstNum = sc.nextInt();
             while(firstNum != 0)
             {

                 System.out.println("second number: ");
                 int secondNum = sc.nextInt();

                 System.out.println("The  sum of your numbers: " + (firstNum + secondNum));

                 System.out.println("first number: ");
                 firstNum = sc.nextInt(); 
             }
         }
    }

public static void main(String[] args)
    {
         try (Scanner sc = new Scanner(System.in)) {

             while(true)
             {
                 System.out.println("first number: ");
                 int firstNum = sc.nextInt();
                 if(firstNum == 0) {
                     break;
                 }
                 System.out.println("second number: ");
                 int secondNum = sc.nextInt();

                 System.out.println("The  sum of your numbers: " + (firstNum + secondNum));

                 System.out.println("first number: ");
                 firstNum = sc.nextInt(); 
             }
         }
    } 

答案 4 :(得分:0)

这很丑陋,但是它会让您了解过程

 package com.company;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        boolean continueRunning = true;
        while (continueRunning) {
            System.out.println("first number: ");
            int firstNum = sc.nextInt();

            System.out.println("second number: ");
            int secondNum = sc.nextInt();

            System.out.println("The  sum of your numbers: " + (firstNum + secondNum));
            continueRunning = firstNum != 0 && secondNum != 0;
        }
    }
}

答案 5 :(得分:0)

也很丑,但今晚我会睡得更好。

      public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        requestNumbersAndSum(scanner);
    }

    private static void requestNumbersAndSum(Scanner scanner) {
        int firstNum = requestANum(scanner, "first number: ");
        int secondNum = requestANum(scanner, "second number: ");
        System.out.println("The  sum of your numbers: " + (firstNum + secondNum));
        requestNumbersAndSum(scanner);
    }

    private static int requestANum(Scanner scanner, String messageToUser) {
        System.out.println(messageToUser);
        int requestedNumber = scanner.nextInt();
        if(requestedNumber == 0){
            System.exit(0);
        }
        return requestedNumber;
    }
}