【问题标题】:for loop nested with switch skips iterations用 switch 嵌套的 for 循环跳过迭代
【发布时间】:2017-01-22 01:06:58
【问题描述】:

我正在构建一个程序,以二维数组的形式存储第 1 天和第 2 天两天的 10 个不同城市(例如城市 1...城市 10)的温度。 但是我的程序正确地接受了第一个输入,然后不读取 switch 语句的第一个条件,并在每次重复时跳过它。

我创建了 3 个类 Main、temp 和 search。 temp 用于存储二维数组中的值,search 用于获取城市名称和对应于 a) 最高温度 b) 最低温度的日期。

package battlefield;

public class Main 
{
 public static void main(String []args)
 {
     temp t=new temp();
     t.takein();
     search s =new search();
     s.sch();
 }
}

package battlefield;

import java.util.Scanner;

public class temp 
{
 int a[][]=new int[2][10];
 String ch;
 Scanner sc=new Scanner(System.in);
 public temp()
 {
 System.out.println("Default temperature have been set to 15 degree  Celsius. ");
    for(int i=0;i<2;i++)
    {
        for(int j=0;j<10;j++)
        {
            a[i][j]=15;
        }
    }
  }
  public void takein()
  {

    for(int i=0;i<2;i++)
    {
        for(int j=0;j<10;j++)
        {
            System.out.println("Do you want to enter more?");
            ch=sc.nextLine();
            sc.nextLine();
            System.out.println("Value of i="+i+" Value of j="+j);
            switch(ch)
            {
                case "y": 
                    {System.out.println("Enter temprature on day "+i+" city "+j);
                    a[i][j]=sc.nextInt();
                    break;}
                case " n":
                    continue;
                case " e":
                     break;
            }
            if (ch.equals("e"))
            {
                break;
            }
        }
        if (ch.equals("e"))
        {
            break;
        }
    }


  }

  }

   package battlefield;

   public class search extends temp
 {
  String rep;
  int t=0;
   public void sch()
 {
    System.out.println("Do you want to search by highest temperature or lowest?");
    rep=sc.nextLine();
    switch(rep)
    {
          case "h":{for(int i=0;i<2;i++)
            {
                for(int j=0;j<10;j++)
                {
                    if(t<a[i][j])
                    {
                        t=a[i][j];
                    }
                    else
                        continue;
                }
            }
          for(int i=0;i<2;i++)
            {
                for(int j=0;j<10;j++)
                {
                    if(t==a[i][j])
                    {
                        System.out.println("City "+j+" has the highest temprature of all on day "+i);
                    }
                    else
                        continue;
                }
            }

          break;}
          case "l":{for(int i=0;i<2;i++)
            {
                for(int j=0;j<10;j++)
                {
                    if(t>a[i][j])
                    {
                        t=a[i][j];
                    }
                    else
                        continue;
                }
            }
            for(int i=0;i<2;i++)
             {
                for(int j=0;j<10;j++)
                {
                    if(t==a[i][j])
                    {
                        System.out.println("City "+j+" has the lowest temperature of all on day "+i);
                    }
                    else
                        continue;
                }
             }
          break;}
          case "n":break;
    }
    }
    }

【问题讨论】:

    标签: java for-loop switch-statement iteration skip


    【解决方案1】:

    nextInt() 留下一个尾随换行符。改变

    ch=sc.nextLine();
    sc.nextLine();
    

    ch=sc.nextLine();
    // sc.nextLine();
    

    并将 sc.nextLine() 移至

    case "y": 
    {
        System.out.println("Enter temprature on day "+i+" city "+j);
        a[i][j]=sc.nextInt();
        sc.nextLine(); // <-- here.
        break;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多