【发布时间】: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