Java 使用开关嵌套的for循环跳过迭代
我正在建立一个程序,以2d数组的形式存储10个不同城市(例如,城市1…城市10)在第1天和第2天的温度。 但是我的程序正确地接收了第一个输入,然后在第一个条件下不读取switch语句,并且每次重复时都跳过它 我创建了3个类:Main、temp和search。 temp用于在2d数组中存储值,搜索用于获取城市名称以及与a)最高温度b)最低温度对应的日期Java 使用开关嵌套的for循环跳过迭代,java,for-loop,switch-statement,iteration,skip,Java,For Loop,Switch Statement,Iteration,Skip,我正在建立一个程序,以2d数组的形式存储10个不同城市(例如,城市1…城市10)在第1天和第2天的温度。 但是我的程序正确地接收了第一个输入,然后在第一个条件下不读取switch语句,并且每次重复时都跳过它 我创建了3个类:Main、temp和search。 temp用于在2d数组中存储值,搜索用于获取城市名称以及与a)最高温度b)最低温度对应的日期 package battlefield; public class Main { public static void main(Strin
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;
}
}
}
package战场;
公共班机
{
公共静态void main(字符串[]args)
{
温度t=新温度();
t、 takein();
搜索s=新搜索();
s、 sch();
}
}
包装战场;
导入java.util.Scanner;
公共类临时工
{
int a[][]=新int[2][10];
弦ch;
扫描仪sc=新的扫描仪(System.in);
公共临时工()
{
System.out.println(“默认温度已设置为15摄氏度”);
对于(int i=0;i而言,正是nextInt()
留下了尾随的换行符。更改
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;
}
案例“y”:
{
System.out.println(“在第天输入温度”+i+“城市”+j);
a[i][j]=sc.nextInt();
sc.nextLine();//正是nextInt()
留下了尾随的换行符。更改
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;
}
案例“y”:
{
System.out.println(“在第天输入温度”+i+“城市”+j);
a[i][j]=sc.nextInt();
sc.nextLine()//