0

经过数小时的研究,我能够想出一个应该有帮助的 while 循环。唯一的问题是它会循环并且不会要求用户再次运行。它只是一遍又一遍地循环高尔夫程序。谁能指出出了什么问题?目前在初级班,所以我可能不太了解。

import java.util.Scanner;

class GolfEM
{ 
    public static void main (String[] args) 
    {
        Scanner kb = new Scanner(System.in);    
        boolean go = true;
        boolean run = true;

        while (go==true)
        {
            while (run)
            {
                System.out.println("Enter par for the hole: ");
                int par = kb.nextInt();
                System.out.println("Enter the number of strokes: ");
                int stroke = kb.nextInt();
                {

                    if (stroke==1)  
                    {
                        System.out.println("hole in one");
                        run=false;
                    }
                    else if (3==par-stroke)
                    {
                        System.out.println("double eagle");
                        run=false;
                    }
                    else if (2==par-stroke)
                    {
                        System.out.println("eagle");
                        run=false;
                    }
                    else if (1==par-stroke)
                    {
                        System.out.println("birdie");
                        run=false;
                    }
                    else if (par==stroke)
                    {
                        System.out.println("par");
                        run=false;
                    }
                    else if (-1==par-stroke)
                    {
                        System.out.println("bogey");
                        run=false;
                    }
                    else if (-2==par-stroke)
                    {
                        System.out.println("double-bogey");
                        run=false;
                    }
                    else if (-3==par-stroke)
                    {
                        System.out.println("triple-bogey");
                        run=false;
                    }
                    else if (-4==par-stroke)
                    {
                        System.out.println("four-over-par");
                        run=false;
                    }
                    else if (-5==par-stroke)
                    {
                        System.out.println("five-over-par");
                        run=false;
                    }
                    else if (-6==par-stroke)
                    {
                        System.out.println("six-over-par");
                        run=false;
                    }
                    else if (-7==par-stroke)
                    {
                        System.out.println("seven-over-par");
                        run=false;
                    }
                    else
                    {
                        System.out.println("Better luck next time");
                        run=false;
                    }
                }
            }
            run=false;
        }
        if (run==false)
            System.out.println("Run again? ");

        char again = kb.next().charAt(0);

        if (again == 'y')
        {
            run=true;
        }
        else if (again == 'n')
        {
            System.out.println("Goodbye.");
            go = false;
        }
        else
        {
            System.err.println("Invalid answer");
        }
    }
}
4

4 回答 4

1

您的代码有一些问题。

  1. 外部 go 循环非常无用,因此完全删除它
  2. 你的 if else 块看起来超级难看。Switch 语句更加简洁
  3. 没有真正的意义来设置 run=false 每种情况,只是在最后做
  4. 您的回复将以无效字符退出

我更改了下面的代码。看一眼。

import java.util.Scanner;
class GolfEM
{ 
    public static void main (String[] args) 
    {
        Scanner kb = new Scanner(System.in);    
        boolean run = true;

        while (run)
        {
            System.out.println("Enter par for the hole: ");
            int par = kb.nextInt();
            System.out.println("Enter the number of strokes: ");
            int stroke = kb.nextInt();                

            if (stroke==1)  {
                System.out.println("hole in one");
                run=false;
            }
            else{
                switch(par-stroke){
                    case 3:
                        System.out.println("double eagle");
                        break;
                    case 2:
                        System.out.println("eagle");
                        break;
                    case 1:
                        System.out.println("birdie");
                        break;

                    case 0:   
                        System.out.println("par");
                        break;
                    case -1:
                        System.out.println("bogey");
                        break;
                    case -2:
                        System.out.println("double-bogey");
                        break;
                    case -3:
                        System.out.println("triple-bogey");
                        break;
                    case -4:
                        System.out.println("four-over-par");
                        break;
                    case -5:
                        System.out.println("five-over-par");
                        break;
                    case -6:
                        System.out.println("six-over-par");
                        break;
                    case -7:
                        System.out.println("seven-over-par");
                        break;
                    default:
                        System.out.println("Better luck next time");

                }

            }                           
            run=false;        

            while(1){
                System.out.println("Run again? ");
                char again = kb.next().charAt(0);
                if (again == 'y')
                {
                run=true;
                break;
                }
                else if (again == 'n')
                {
                System.out.println("Goodbye.");
                break;
                }
                else
                {
                    System.err.println("Invalid answer");
                }
            }
        }
}

}
于 2013-02-22T15:47:13.017 回答
0

如果我能给出一些指示,那么这里不需要两个 while 循环。你真的只需要一个(比如说,外部的,'go' 循环)。

你的逻辑应该是这样的......

    boolean go = true;
    while (go) {
         // get input...
         // compute the result for this hole...
         // get user input...
         // decide whether to go again (go = false; or go = true;)
    }

小心花括号。像其他人建议的那样,首先正确缩进您的代码。

于 2013-02-22T15:38:56.280 回答
0

正如其他成员所建议的,使用体面的缩进。

在这里,您只是放错了几个大括号。

更正的代码:

import java.util.Scanner;
class GolfEM
{
public static void main (String[] args)
{
Scanner kb = new Scanner(System.in);
boolean go = true;
boolean run = true;
while (go==true)
    {
    while (run)
        {
System.out.println("Enter par for the hole: ");
int par = kb.nextInt();
System.out.println("Enter the number of strokes: ");
int stroke = kb.nextInt();


if (stroke==1)  {
System.out.println("hole in one");
run=false;
        }
else
if (3==par-stroke){
System.out.println("double eagle");
run=false;
        }
else
if (2==par-stroke){
System.out.println("eagle");
run=false;
        }
else
if (1==par-stroke){
System.out.println("birdie");
run=false;
        }
else
if (par==stroke){
System.out.println("par");
run=false;
        }
else
if (-1==par-stroke){
System.out.println("bogey");
run=false;
        }
else
if (-2==par-stroke){
System.out.println("double-bogey");
run=false;
        }
else
if (-3==par-stroke){
System.out.println("triple-bogey");
run=false;
        }
else
if (-4==par-stroke){
System.out.println("four-over-par");
run=false;
        }
else
if (-5==par-stroke){
System.out.println("five-over-par");
run=false;
        }
else
if (-6==par-stroke){
System.out.println("six-over-par");
run=false;
        }
else
if (-7==par-stroke){
System.out.println("seven-over-par");
run=false;
        }
else{
System.out.println("Better luck next time");
run=false;
        }
           run=false;
        }


    if (run==false)
    System.out.println("Run again? ");
    char again = kb.next().charAt(0);
        if (again == 'y')
        {
        run=true;}
        else if (again == 'n')
        {
        System.out.println("Goodbye.");
        go = false;}
        else
        {
        System.err.println("Invalid answer");
        }}
    }
  }
于 2013-02-22T15:47:09.663 回答
0

我真的不明白内run循环是干什么用的。它只运行一次,然后run设置为 false。但是,我假设您的代码是初步的,这部分将在以后填写。

关于您的实际问题:您的程序在无限循环中运行,因为询问用户是否再次运行程序的部分在循环之外。go只需将第一部分“花括号”向上移动就可以了。

于 2013-02-22T15:49:22.417 回答