提示用户继续不工作,java

时间:2015-04-24 06:25:35

标签: java loops do-loops

我需要帮助。我想询问用户是否要再试一次,但我的代码似乎有些问题,因为它无效。

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

      Scanner keyboard = new Scanner(System.in);

      int n1, n2, sum;
      String answer;
      do {

      System.out.println("Enter number 1: ");
      n1 = keyboard.nextInt();

      System.out.println("Enter number 2: ");
      n2 = keyboard.nextInt();

      sum = n1 + n2;

      System.out.println("Number 1\t" + "Number 2\t" + "Sum");
      System.out.println("__________________________________");
      System.out.println(n1 + "\t\t" + n2 + "\t\t" + sum);

      System.out.println("Enter yes to continue or any other key to end");
      answer = keyboard.nextLine();

      keyboard.nextLine();

      }
      while(answer.equalsIgnoreCase("YES"));



}

   }

当我运行它时,它会存储用户的答案,但程序不会重复。我该如何解决这个问题?

3 个答案:

答案 0 :(得分:2)

location / { proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-NginX-Proxy true; proxy_redirect off; proxy_pass http://internalhost:8080/; } 之后移动function generateGraph(data) { var dates = new Array(); var startDate = ""; for (var i = 0; i < data.length; i++) { dates[i] = data[i].date; } var productInsight = _comm.getProductInsightArray(); var productInsight_Array = new Array(); $.each(productInsight, function(key, value) { productInsight_Array.push(key); }); if(dates.length > 0) { startDate = dates[0].split("/"); } intervals = 24 *3600 * 1000; //one day var title = $("#DateSearch_analytics_Similar_Stores1").val(); var color = ['#000000', '#FFFFFF', '#000080', '#0000FF', '#808080']; var containers =['container1', 'container2','container3','container4', 'container5', 'container6', 'container7', 'container8', 'container9']; for(var j=0; j<containers.length; j++) { var chart = new Highcharts.Chart({ chart: { zoomType: 'x', renderTo: containers[j], type: 'line', }, title: { text: title }, subtitle: { text: document.ontouchstart === undefined ? 'Click and drag in the plot area to zoom in' : 'Pinch the chart to zoom in' }, xAxis: { type: 'datetime', tickInterval: intervals, }, series: [] }); for(var i=0; i < productInsight_Array.length; i++) { var fillData = new Array(); var counter = 0; var productValue = document.getElementById('product-nm').value; $.each(dates, function() { fillData[counter] = _comm.randomNumberFromRange(_randomNumberStartRange, _randomNumberEndRange); counter++; }); chart.addSeries({ name: productInsight_Array[i], type: 'line', color: color[i], pointStart: Date.UTC(startDate[2], startDate[0] - 1, startDate[1]), pointInterval: intervals, data: fillData, }); } $("text:contains('Highcharts.com')").css("display", "none"); } } 接受并忽略输入流中留下的悬空换行符,并致电keyboard.nextLine();

当我运行它时,它会存储用户的答案 - 尝试打印存储在n2 = keyboard.nextInt();字段中的内容,然后您就会看到问题。

答案 1 :(得分:0)

更改keyboard.nextLine();的位置。

keyboard.nextLine();
answer = keyboard.nextLine();

在你的代码中,答案是获得下一行(即enter),当你获取n2的值并按回车键时,它会进入画面。

您可以通过执行以下代码

来测试您的代码
System.out.println("Enter yes to continue or any other key to end");
answer = keyboard.nextLine();
System.out.println("Answer : " + answer);
System.out.println(keyboard.nextLine());

答案 2 :(得分:0)

Scanner keyboard = new Scanner(System.in);

      int n1, n2, sum;
      String answer = "Yes";

while (answer.equals("Yes"))
{
 System.out.println("Enter number 1: ");
      n1 = keyboard.nextInt();

      System.out.println("Enter number 2: ");
      n2 = keyboard.nextInt();

      sum = n1 + n2;

      System.out.println("Number 1\t" + "Number 2\t" + "Sum");
      System.out.println("__________________________________");
      System.out.println(n1 + "\t\t" + n2 + "\t\t" + sum);

      System.out.println("Enter yes to continue or any other key to end");
      answer = keyboard.nextLine();

      keyboard.nextLine();
}