如何在用户准备结束之前重新编码请求用户输入的主方法

时间:2014-03-22 18:39:32

标签: java loops

我试图无限地重复以下主要方法,以便在第一次运行后再次询问用户输入,并在用户退出之前一遍又一遍地执行相同操作:

import java.util.Scanner;

public class methodselection {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System. in );
        System.out.println("Please Enter A String of any length");

        String text1;
        int n1;
        text1 = scanner.nextLine();

        System.out.println("Please choose a computation to run ");
        System.out.println("To Choose a computation, type in the correpsonding number to the computation you want ");
        System.out.println("The choices are: 1 = \"check if palidrone\" , 2 = \"compute rounded sum \" , 3 = \"count unique characters\" ");

        n1 = scanner.nextInt();
        if (n1 == 1) {
            System.out.println("You chose \"check if palindrone\" ");
            System.out.println(isPalindrome(text1));
        } else {
            if (n1 == 2) {
                System.out.println("You chose \" compute rounded sum \" ");
                System.out.println(roundedsum(text1));
            } else {
                if (n1 == 3) {
                    System.out.println("You chose \" count unique characters\" ");
                    System.out.println(countUniqueCharacters(text1));
                }
            }
        }
    }
}

5 个答案:

答案 0 :(得分:1)

将主要方法中的所有代码放在

while (true or some condition)
{
    //your code here
}

答案 1 :(得分:0)

while(true){

//write your code here.

}

答案 2 :(得分:0)

如果你真的想无限循环,你可以使用while循环:

while (true) {
    // ...
}

您可能希望在循环之前放置一些代码,就像只需要声明一次的变量一样:

Scanner scanner = ...;

String text1;
int n1;

while(true) { ... }

while循环通常与条件一起使用,例如:

while (n1 != -1) { ... }

因此,当用户输入-1时,循环将退出。

如果您使用循环,则会出现问题。会发生什么情况nextInt()方法不会消耗按 Enter 时输入的换行符,因此您需要使用它(否则nextLine()您用来获取文本将使用它):

n1 = scanner.nextInt();
scanner.nextLine(); // consume new-line character

答案 3 :(得分:0)

您可以使用while循环,并将check始终设置为true。

import java.util.Scanner;
public class methodselection {

public static void main(String[] args) {

Scanner scanner = new Scanner (System.in);

System.out.println("Please Enter A String of any length");

String text1;

int n1;

text1 = scanner.nextLine();


System.out.println("Please choose a computation to run ");
System.out.println("To Choose a computation, type in the correpsonding number to the computation you want ");
System.out.println("The choices are: 1 = \"check if palidrone\" , 2 = \"compute rounded sum \" , 3 = \"count unique characters\" ");

while(true){
    n1 = scanner.nextInt();

    if (n1 == 1){
        System.out.println("You chose \"check if palindrone\" ");
        System.out.println(isPalindrome(text1));

        }


    else{
        if(n1 == 2){
                System.out.println("You chose \" compute rounded sum \" ");
                System.out.println(roundedsum(text1));
        }   
        else{
            if(n1 == 3){
                System.out.println("You chose \" count unique characters\" ");
                System.out.println(countUniqueCharacters(text1));
            }
        }
    }   
    }
}

while循环结构如下:

while(condition){
    // do this code
}

只要条件成立,循环中的代码将继续执行。通过将值设置为" true"条件总是正确的。

答案 4 :(得分:0)

import java.util.Scanner;
public class methodselection { 

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

System.out.println("Please Enter A String of any length");

String text1;

 int n1;

 text1 = scanner.nextLine();


System.out.println("Please choose a computation to run ");
System.out.println("To Choose a computation, type in the correpsonding number to the      computation you want ");
System.out.println("The choices are: 1 = \"check if palidrone\" , 2 = \"compute rounded sum \" , 3 = \"count unique characters\" ");


n1 = scanner.nextInt();

if (n1 == 1){
    System.out.println("You chose \"check if palindrone\" ");
    System.out.println(isPalindrome(text1));

    }


else{
    if(n1 == 2){
        System.out.println("You chose \" compute rounded sum \" ");
        System.out.println(roundedsum(text1));
    }   
    else{
        if(n1 == 3){
            System.out.println("You chose \" count unique characters\" ");
            System.out.println(countUniqueCharacters(text1));
        }
    }
main(null);//it will throw  run time error (StackOverflowError) //
}   
}