如何在java中使用Squaring应用程序?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (38)

这是编程新手。我需要编写执行以下操作的应用程序 ... 平方应用程序说明

我到目前为止的代码如下。我遇到了一个问题,我的代码不会从负整数读取到字符串并正确提示用户输入有效数据。我相信我需要嵌套我的循环,但我很难这样做。

import java.util.Scanner;

public class Squaring {

    public static int getValidInt(int greaterThan, Scanner scan) {
        System.out.println("Enter an integer greater than " + greaterThan + ":" );
        int input; 

        while ( !scan.hasNextInt() ) { 
            String garbage = scan.next();
            scan.nextLine(); 
            System.out.println(garbage + " is not valid input."); 
            System.out.println("Enter an integer greater than " + greaterThan + ":" );
        }

        while ( !((input = scan.nextInt()) > greaterThan )) { 
            int garbage = input; 
            System.out.println(garbage + " is not greater than 1.");
            System.out.println("Enter an integer greater than " + greaterThan + ":" );
        }

        return input; 
    }


    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int a = 1 ;
        int total = 0;
        a = getValidInt(a,scan);
        int b = a; 
        System.out.println(a);
        long n = a; 
        while ( n < 1000000 ) {
            System.out.println(n*n);
            n = n * n; 
            total = total + 1; 
        }
        System.out.println(b + " exceeded 1,000,000 after " + total + " squarings.") ;
    }
}
提问于
用户回答回答于

我想出了以下内容:

public static int getValidInt(int greaterThan, Scanner scan) {

    int number = greaterThan;

    do {
        while (!scan.hasNextInt()) {
            String garbage = scan.next();
            System.out.println(garbage + " is not valid input.");
        }
        number = scan.nextInt();

        if (number < greaterThan) {
            System.out.println("input is: " + number + " minimum is: " + greaterThan);
        }

    } while (number < greaterThan);

    return number;
}

它有一个do while循环,确保number不能小于greaterThan,如果是它将while再次循环。do while循环内部是另一个循环,告诉用户他们的输入是垃圾,并将继续这样做,直到插入一个数字。

用户回答回答于

没有任何try-catch:

public static int getValidInt(int greaterThan, Scanner scan) {
    int input = 0;
    boolean valid = false;

    while (!valid) {
        System.out.println("Enter an integer greater than " + greaterThan + ":");

        if (!scan.hasNextInt()) {
            String garbage = scan.next();
            System.out.println(garbage + " is not valid input.");
        } else {
            input = scan.nextInt();

            if (input <= greaterThan) {
                System.out.println(input + " is not greater than " + greaterThan);
            } else {
                valid = true;
            }
        }
    }

    return input;
}

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

    int a = getValidInt(1, scan);
    System.out.println(a);

    int total = 0;
    long n = a;

    while ( n < 1000000 ) {
        n = n * n;
        System.out.println(n);
        total++;
    }

    System.out.println(a + " exceeded 1,000,000 after " + total + " squarings.") ;
}

扫码关注云+社区

领取腾讯云代金券