什么时候做出是或否回答? java的

时间:2014-02-22 20:59:18

标签: java

我目前的目标是让这行代码继续重新运行,只要“是”为真,或者如果它是“否”则停止,或者如果它无效则提示用户进行正确选择。我完全迷失了该怎么做。

    import java.util.Scanner;

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

        Scanner input = new Scanner (System.in);
        System.out.println("Enter a number, a space and F to convert from degF to degC and C to convert to degC to degF");
        double deg = input.nextDouble();
        String letter = input.next().toUpperCase();
        char dg = letter.charAt(0);
        //String prompt;
       tempLoop:
        do{
            if( dg == 'C'){
                double cTemp= (deg-32)*5.0/9.0;
                System.out.printf("%f degC converted to degF is %.2f%n",deg, cTemp );;
            }
            else if(dg == 'F'){
                double fTemp = (deg*9/5) + 32;
                System.out.printf("%f degF converted to degC is %.2f%n",deg, fTemp );
                continue;
            }
            else{
                System.out.println("That character does not correspond to a valid unit of measure ");
                break;
            }
        }while(!(dg == 'C' || dg == 'F'));
        System.out.println("Would you like to continue?(yes/no)"); 
      }
     }

5 个答案:

答案 0 :(得分:2)

我稍微更新了你的代码。您现在可以键入15F,15C,...(无空格)进行转换。在此之后,系统会询问您是否要继续。

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

        do {
            System.out
                    .println("Enter a number and F to convert from degF to degC or C to convert to degC to degF");
            String text = input.next().toUpperCase();

            try {
                deg = Double.parseDouble(text.substring(0, text.length() - 1));
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }

            if (text.endsWith("C")) {
                double cTemp = (deg - 32) * 5.0 / 9.0;
                System.out.printf("%f degC converted to degF is %.2f%n", deg, cTemp);
                ;
            } else if (text.endsWith("F")) {
                double fTemp = (deg * 9 / 5) + 32;
                System.out.printf("%f degF converted to degC is %.2f%n", deg, fTemp);
                continue;
            } else {
                System.out
                        .println("That character does not correspond to a valid unit of measure ");
            }
            System.out.println("Type YES to continue");
        } while (input.next().equalsIgnoreCase("YES"));
        input.close();

    }

答案 1 :(得分:1)

你只输入一次USER输入,所以我不确定你为什么使用do-while循环。如果你试图在YES \ NO的基础上迭代,那么在{{1}中添加输入循环并添加一个关于YES / NO的输入,并在while时添加此检查。

使用以下代码:

do

答案 2 :(得分:0)

您需要更改循环以包含用户提示代码并进行更改,以便在用户键入“yes”后再次进行更改。像这样:

import java.util.Scanner;

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

    Scanner input = new Scanner (System.in);
    double deg;
    char dg;
    String prompt;
   tempLoop:
    do{
        System.out.println("Enter a number, a space and F to convert from degF to degC and C to convert to degC to degF");
        deg = input.nextDouble();
        String letter = input.next().toUpperCase();
        dg = letter.charAt(0);
        if( dg == 'C'){
            double cTemp= (deg-32)*5.0/9.0;
            System.out.printf("%f degC converted to degF is %.2f%n",deg, cTemp );;
        }
        else if(dg == 'F'){
            double fTemp = (deg*9/5) + 32;
            System.out.printf("%f degF converted to degC is %.2f%n",deg, fTemp );
            continue;
        }
        else{
            System.out.println("That character does not correspond to a valid unit of measure ");
            break;
        }
        System.out.println("Would you like to continue?(yes/no)");
        prompt = input.next();
    }while(prompt.equalsIgnoreCase("yes")); 
  }
 }

答案 3 :(得分:0)

您希望在再次启动循环之前询问用户的输入。这样,在循环结束时,它询问用户他/她是否想要继续。如果答案为“是”,则代码将继续循环。如果答案为“否”,程序将退出循环并继续。

import java.util.Scanner;

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

    Scanner input = new Scanner (System.in);
    System.out.println("Enter a number, a space and F to convert from degF to degC and C to convert to degC to degF");
    double deg = input.nextDouble();
    String letter = input.next().toUpperCase();
    char dg = letter.charAt(0);
    //String prompt;
   tempLoop:
    do{
        if( dg == 'C'){
            double cTemp= (deg-32)*5.0/9.0;
            System.out.printf("%f degC converted to degF is %.2f%n",deg, cTemp );;
        }
        else if(dg == 'F'){
            double fTemp = (deg*9/5) + 32;
            System.out.printf("%f degF converted to degC is %.2f%n",deg, fTemp );
            continue;
        }
        else{
            System.out.println("That character does not correspond to a valid unit of measure ");
            break;
        }

        System.out.print("Would you like to continue?(yes/no) ");
        String cont = input.nextLine();
    }while((!(dg == 'C' || dg == 'F')) && cont.equalsIgnoreCase("yes"));

  }
 }

答案 4 :(得分:0)

import java.util.Scanner;

public class Temperature
{

private static String reply;
 public static void main(String[] args){

    Scanner input = new Scanner (System.in);
    System.out.println("Enter a number, a space and F to convert from degF to degC and C to convert to degC to degF");
    double deg = input.nextDouble();
    String letter = input.next().toUpperCase();
    char dg = letter.charAt(0);
    //String prompt;
   tempLoop:
   do{ 
   do{
        if( dg == 'C'){
            double cTemp= (deg-32)*5.0/9.0;
            System.out.printf("%f degC converted to degF is %.2f%n",deg, cTemp );;
        }
        else if(dg == 'F'){
            double fTemp = (deg*9/5) + 32;
            System.out.printf("%f degF converted to degC is %.2f%n",deg, fTemp );
            continue;
        }
        else{
            System.out.println("That character does not correspond to a valid unit of measure ");
            break;
        }
    }while(!(dg == 'C' || dg == 'F'));
    System.out.println("Would you like to continue?(yes/no)"); 
    reply=input.next().toUpperCase();
   }while(reply.equals("YES"));
  }
 }