我需要帮助完成作业

时间:2011-05-16 17:57:53

标签: java

我已经在这个节目上工作了大约3个星期,现在无法弄清楚我的错误。我必须使用两个公共类:1)validateLength(Number)和2)convertIntegerToWords(Number)。我的问题是,一旦用户输入他们的整数,我的循环将永远继续。系统将要求整数,用户输入,系统输出太长或继续convertIntgerToWords。我的代码在

之下
import java.util.Scanner;
public class Project2 {

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

        //Create a Scanner//
        Scanner input = new Scanner(System.in);

        //Enter an Integer//   
        System.out.print(" What is your integer ? ");
        int Number= input.nextInt(); 
        while (Number >= 0) {
            if (Number != 0)
                validateLength(Number);
            else if(Number == 0) {
                System.out.print( "Thank you for playing! " + "Good bye! ");    
                break;
            }
        }   
    }

    //Next Method//
    public static boolean validateLength(int userNum) {
        String Number = "" + userNum;
        while (userNum >= 0) {
            if (userNum < 10) 
                convertIntegerToWords(userNum);
            else if (userNum > 9){
                System.out.print("Your integer is too long !");
                break;
            }
        }
    }
    //End of validate//

    //Final Method//
    public static String convertIntegerToWords(int Number) {
        if (Number == 1)
            System.out.println("Your integer " + Number + "is written out as one");
        else if (Number == 2) 
            System.out.println("Your integer " + Number + "is written out as two");
        else if (Number == 3) 
            System.out.println("Your integer " + Number + "is written out as three");
        else if (Number == 4) 
            System.out.println("Your integer " + Number + "is written out as four");
        else if (Number == 5) 
            System.out.println("Your integer " + Number + "is written out as five");
        else if (Number == 6) 
            System.out.println("Your integer " + Number + "is written out as six");
        else if (Number == 7) 
            System.out.println("Your integer " + Number + "is written out as seven");
        else if (Number == 8) 
            System.out.println("Your integer " + Number + "is written out as eight");
        else if (Number == 9) 
            System.out.println("Your integer " + Number + "is written out as nine");
            return Number + "";
        }
    }
}

6 个答案:

答案 0 :(得分:3)

你需要移动

Number = input.nextInt();

while循环内部。这是典型的习语(也增加了其他清理):

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

    // Enter an Integer//
    System.out.print(" What is your integer ? ");
    int Number;
    while ((Number = input.nextInt()) >= 0)
    {
        if (Number == 0)
        {
            System.out.print("Thank you for playing! " + "Good bye! ");
            break;
        }

        validateLength(Number);
    }
}

修改

  

如果用户输入0,那么程序终止。但是,如果用户输入1-9的整数,程序应该用单词拼出整数(即1表示为1)。它这样做但它循环无限。就像用户输入大于9的整数一样,它报告“YOur整数太大,输入另一个整数”然而,这反复在同一行重复。

这是因为while中的validateLength()循环。试试这个(注意其他代码清理):

public class ScannerDemo
{
    private static void convertIntegerToWords(int num)
    {
        String message = null;
        if (num > 9)
        {
            message = "Your integer is too long!";
        }
        else if (num > 0)
        {
            message = "Your integer " + num + " is written out as ";
            String numString = "";

            switch (num)
            {
                case 1:
                    numString = "one"; break;
                case 2:
                    numString = "two"; break;
                case 3:
                    numString = "three"; break;
                case 4:
                    numString = "four"; break;
                case 5:
                    numString = "five"; break;
                case 6:
                    numString = "six"; break;
                case 7:
                    numString = "seven"; break;
                case 8:
                    numString = "eight"; break;
                case 9:
                    numString = "nine"; break;
            }

            message += numString;
        }

        System.out.println(message);
    }

    private static int getNextNumber(Scanner s)
    {
        System.out.println("What is your integer?");
        return s.nextInt();
    }

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

        int number;
        while ((number = getNextNumber(input)) >= 0)
        {
            if (number == 0)
            {
                System.out.println("Thank you for playing! Good bye!");
                break;
            }

            convertIntegerToWords(number);
        }
    }
}

它也是on github

答案 1 :(得分:0)

你的条件总是满足,所以它将继续通过while循环。它只会在Number不再大于或等于零时停止。

你需要完全使用其他一些条件,如果你想使用一个循环(虽然我不知道为什么你想在这个例子中)。您永远不会更改与Number关联的值,因此它始终是实例化的任何值。也许你的意思是让代码在一定条件下改变它的价值?如果没有,你需要完全失去这个条件。

答案 2 :(得分:0)

while (Number >= 0) {

正在创建一个while循环,其中我没有看到你减少Number整数来停止循环。你需要在这里使用循环吗?您应该尝试使用if语句。

此外,您可能需要考虑使用switch语句而不是输出。

答案 3 :(得分:0)

欢迎使用StackOverflow。

我觉得需要提一下你有一个班级Project2和两个方法而不是班级。你得到一个int而不是一个整数而你没有读到我认为是你问题的下一行,除非你希望用户在第一行输入所有内容。

我建议你学习使用调试器,因为这对于查找调试和理解你的程序正在做什么非常有用。 esp表示不结束的循环。

答案 4 :(得分:0)

//Main Method//
public static void main(String [] args) {
    //Create a Scanner//
    Scanner input = new Scanner(System.in);
    bool bPlay = true; 

    while (bPlay) {
        //Enter an Integer//   
        System.out.print(" What is your integer ? ");
        int Number= input.nextInt(); 

        if (Number != 0)
        validateLength(Number);

        else if(Number == 0) {
            System.out.print( "Thank you for playing! " + "Good bye! "); 
            bPlay = false; 
            break;

        }
    }   
}

答案 5 :(得分:0)

  public static boolean validateLength(int userNum) {
    String Number = "" + userNum;
        while (userNum >= 0) {

    if (userNum < 10) 
    convertIntegerToWords(userNum);

    else if (userNum > 9){
    System.out.print("Your integer is too long !");
            break;
    }
  }

此代码负责此问题,您永远不会为userNum分配新值,而是在userNum&gt; 0时循环。这为数字> = 0和<10的无限循环创造了一个最佳点。 (你为了数字> 9而离开循环。)