Java 为什么我的输出与另一个输出不同?
我的输出正在与另一对输出分组,而该输出根本不会打印。 根本不起作用的输入是(f,i),分组对输入是(c,c) *注意,我认为它与Java 为什么我的输出与另一个输出不同?,java,Java,我的输出正在与另一对输出分组,而该输出根本不会打印。 根本不起作用的输入是(f,i),分组对输入是(c,c) *注意,我认为它与if()语句有关 import java.util.Scanner; class AnnualClimate{ public static void main (String [] args) { //Declare and intialize variables - programmer to provide initial values Scanner i
if()
语句有关
import java.util.Scanner;
class AnnualClimate{
public static void main (String [] args)
{ //Declare and intialize variables - programmer to provide initial values
Scanner in = new Scanner(System.in);
String city = "Daytona Beach";
String state = "Florida";
int o=0;
int u=0;
int y=0;
int t=0;
int z=0;
int m=0;
double [] celsius;
int i = 0;
int index=0;
double [] average1c;
double [] average2c;
double [] average1;
double [] average2;
String month [] ={"Jan", "Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
double temperature [] = {58.4,60.0,64.7,68.9,74.8,79.7,81.7,81.5,79.9,74.0,67.0,60.8};
double precipitation [] ={3.1, 2.7,3.8,2.5,3.3,5.7,5.2,6.1,6.6,4.5,3.0,2.7};
String tempLabel = "F"; //initialize to F
String precipLabel = "inch"; //initialize to inch
//INPUT - ask user for temp and preciptation scale choice
System.out.print("Choose the temperature scale (F = Fahrenheit, C = Celsius): ");
String tempChoice = in.next();
System.out.print("Choose the precipitation scale (i = inches, c = centimeteres): ");
String precipChoice = in.next();
//PROCESSING - convert from F to C and in to cm based on user's choices
// remember 5/9 = 0, 5.0/9 = .5555
average1c = new double[temperature.length];
celsius = new double[temperature.length];
average2c = new double[precipitation.length];
average1 = new double[temperature.length];
average2 = new double[temperature.length];
if(tempChoice.equalsIgnoreCase("C")){
tempLabel="(C)";
for( index = 0; index < temperature.length;)
{
celsius[index]= (temperature[index]-32) * 5/9;
average1[index]= celsius[index]/12;
index++;
}
}
//Convert in values to cm; replace the current values in precipitation
double[] centimeters = new double[ precipitation.length ];
if(precipChoice.equalsIgnoreCase("c"))
{
precipLabel="(cm)";
for ( i= 0; i<precipitation.length; i++)
{
centimeters[i]= precipitation[i]* 2.54;
average2[i]=centimeters[i]/12;
}
}
for( z=0; z < temperature.length; z++)
{
average1[z]= temperature[z]/12;
}
for( m=0; m < temperature.length; m++)
{
average1[m]= temperature[m]/12;
}
//OUTPUT - print table using printf to format and align data
System.out.println();
System.out.println("Climate Data");
System.out.println("Location: " + city +", " + state);
System.out.printf("%5s %18s %s %18s %s","Month","Temperature",tempLabel,"Precipitation",precipLabel);
System.out.printf("%n");
System.out.printf("***************************************************");
while ( o< month.length){
if(tempChoice.equalsIgnoreCase("C")){
System.out.printf("%n");
System.out.printf(month[o]);
System.out.printf("%20.2f", celsius[o]);
System.out.printf("%25.2f", precipitation[o]);
o++;
}
}
while ( u< month.length){
if(tempChoice.equalsIgnoreCase("c") & (precipChoice.equalsIgnoreCase("c"))){
System.out.printf("%n");
System.out.printf(month[u]);
System.out.printf("%20.2f", celsius[u]);
System.out.printf("%25.2f", centimeters[u]);
u++;
}
}
while (y< month.length){
if(tempChoice.equalsIgnoreCase("f") & (precipChoice.equalsIgnoreCase("c")))
{
System.out.printf("%n");
System.out.printf(month[y]);
System.out.printf("%20.2f",temperature [y]);
System.out.printf("%25.2f", centimeters[y]);
y++;
}
}
while ( t< month.length){
if(tempChoice.equalsIgnoreCase("f") & (precipChoice.equalsIgnoreCase("i"))){
System.out.printf("%n");
System.out.printf(month[t]);
System.out.printf("%20.2f",temperature [t]);
System.out.printf("%25.2f", precipitation[t]);
t++;
}
}
System.out.println();
System.out.printf("***************************************************");
System.out.println();
while (y< month.length)
{
if(tempChoice.equalsIgnoreCase("f") & (precipChoice.equalsIgnoreCase("c")))
{
System.out.printf("%n");
System.out.printf("%20.2f", average1 );
System.out.printf("%25.2f", average2c);
y++;
}
}
import java.util.Scanner;
类年气候{
公共静态void main(字符串[]args)
{//声明并初始化变量-程序员提供初始值
扫描仪输入=新扫描仪(系统输入);
String city=“代托纳海滩”;
字符串state=“佛罗里达”;
INTO=0;
int u=0;
int y=0;
int t=0;
int z=0;
int m=0;
双[]摄氏度;
int i=0;
int指数=0;
双[]平均1c;
双[]平均2c;
双[]平均值1;
双[]平均值2;
字符串月[]={“一月”、“二月”、“三月”、“四月”、“五月”、“六月”、“七月”、“八月”、“九月”、“十月”、“十一月”、“十二月”};
双温[]={58.4,60.0,64.7,68.9,74.8,79.7,81.7,81.5,79.9,74.0,67.0,60.8};
双重降水[]={3.1,2.7,3.8,2.5,3.3,5.7,5.2,6.1,6.6,4.5,3.0,2.7};
字符串tempLabel=“F”//初始化为F
字符串precipLabel=“inch”//初始化为inch
//输入-要求用户选择温度和沉淀刻度
System.out.print(“选择温度刻度(F=华氏度,C=摄氏度):”;
字符串tempChoice=in.next();
System.out.print(“选择降水量刻度(i=英寸,c=厘米):”;
字符串precipChoice=in.next();
//处理-根据用户的选择从F转换为C,从in转换为cm
//记住5/9=0,5.0/9=0.5555
average1c=新的双[温度.长度];
摄氏度=新的双倍[温度.长度];
average2c=新的双[降水量.长度];
平均值1=新的双[温度.长度];
平均值2=新的双[温度.长度];
if(tempChoice.equalsIgnoreCase(“C”)){
tempLabel=“(C)”;
对于(指数=0;指数<温度.长度;)
{
摄氏[指数]=(温度[指数]-32)*5/9;
平均1[指数]=摄氏[指数]/12;
索引++;
}
}
//将值转换为cm;替换降水中的当前值
双[]厘米=新的双[降水量.长度];
if(精确选择等信号情况(“c”))
{
precipLabel=“(厘米)”;
对于(i=0;i这是调试器的作业
您的第一个问题是“月”循环
然后使用一个好的IDE,添加一些断点并开始调试代码……这是一项非常宝贵的技能,您应该尽快开始学习;)这是大量代码,几乎没有方向。为了帮助我们提供及时、彻底的答案,我们希望尽可能少的代码来复制您的错误,并描述应用程序试图实现的目标(而不仅仅是输出)。(f,i)
和(c,c)
可能对您有意义,但对我们没有任何意义。@用户下次,请删除所有不需要的空格,并以正确的格式显示代码。这样更容易阅读。
while (o < month.length) {
if (tempChoice.equalsIgnoreCase("C")) {
System.out.printf("%n");
System.out.printf(month[o]);
System.out.printf("%20.2f", celsius[o]);
System.out.printf("%25.2f", precipitation[o]);
o++;
}
}
for (o = 0; o < month.length; o++) {
if (tempChoice.equalsIgnoreCase("C")) {
System.out.printf("%n");
System.out.printf(month[o]);
System.out.printf("%20.2f", celsius[o]);
System.out.printf("%25.2f", precipitation[o]);
}
}