一个“如果”声明正在被触发,即使它不应该被触发,无论我如何尝试修复它 [英] One "if" statement is being triggered even though it should not be, regardless of how I try to fix it

查看:104
本文介绍了一个“如果”声明正在被触发,即使它不应该被触发,无论我如何尝试修复它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对编码有些陌生,在我的课堂上,我们的任务是制作一个程序来进行三种类型的转换。当我运行这个程序时,它的大部分方面都可以正常工作。所有命令运行正常,但每当我通过命令(例如:Money)并退出GUI窗口时,程序运行if语句为null但仍然运行弹出警告的命令GUI。我无法弄清楚它为什么这样做(我的老师也不能这样做),此时,我只是想知道为什么警告GUI总是激活而不是如何修复它。



Ps:我知道有更简单的方法来完成我想做的事情,但在我写这篇文章的时候,我很新。这个东西在这一点上是一团糟,可能仍然有失败的尝试来解决这个问题,对不起。



继承人代码:



I am somewhat new to coding, and in my class, we were tasked with making a program to do three types of conversions. When I run this program, most aspects of it work perfectly fine. All of the commands run fine, but whenever I go through a command (ex: "Money") and exit out of the GUI window, the program runs the if statement for "null" but also still runs the one that pops up the warning GUI. I cannot figure out why it is doing this (neither could my teacher) and at this point, I just want to know why the warning GUI is always activating and not how to fix it.

Ps: I know that there are easier ways to accomplish what I wanted to do, but at the point in time when I wrote this I was very new. Also this stuff is a mess at this point and there may still be remnants of failed attempts to fix the issue, sorry.

Heres the code:

import javax.swing.JOptionPane;
import java.util.Scanner;
import java.text.DecimalFormat;

public class Conversion {

	public static void main(String[] args) {
			
		DecimalFormat rnd2= new DecimalFormat("0.00");
		DecimalFormat rnd5= new DecimalFormat("0.00000");
		Scanner reader = new Scanner(System.in);
		
		double m, m1, m2, m3, i, cm, s, w, wr, d, dr, h, hr, min, minr, sec;
		String ask = "null",loop = "true";

	if(ask.equals("null")) {
		System.out.println("Do you want to convert Money, Inches, Seconds, or All? End the program by typing \"End\".");
			ask = reader.nextLine();
	}
	if(ask.equals("End")) {
		loop = "false";
		reader.close();
	}

	else {
		
		while(loop.equals("true")) {
			
			if(!ask.equals("All") && !ask.equals("Money") && !ask.equals("Inches") && !ask.equals("Seconds") && !ask.equals("End") && !ask.equals("null")){

				loop = "true";	
				ask = JOptionPane.showInputDialog(null,"Please Enter \"All\", \"Money\", \"Inches\", \"Seconds\", or type \"End\" to end the program.","WARNING",JOptionPane.WARNING_MESSAGE);
				
			}

			else if(ask.equals("All")) {
				System.out.println("Enter your money as \"Dollar.Cents\" in U.S. Dollars, then your inches, then seconds");
				m = reader.nextDouble();
				i = reader.nextDouble();
				s = reader.nextDouble();
				
				m1 = m*0.86;	//Euro
				m2 = m*0.00016;	//BitCoin
				m3 = m*19.3;	//Peso
				
				cm = i*2.54;
				/* Super Sweaty Conversion Mumbo Jumbo */
				w = (int)s/604800;
				wr = s - w*604800;
				d = (int)wr/86400;
				dr = wr - d*86400;
				h = (int)dr/3600;
				hr = dr - h*3600;
				min = (int)hr/60;
				minr = hr - min*60;
				sec = (int)minr;
				
				ask = "null";
				loop = "true";
				JOptionPane.showMessageDialog(null,
						"Your $"+m+" is equal to\n"+rnd2.format(m1)+" Euros\n"+rnd5.format(m2)+" BitCoins\n"+rnd2.format(m3)+" Pesos\n\n"+
						i+" inches is eqaul to "+cm+" centimeters\n\n"+
						s+" seconds is equal to "+w+" weeks, "+d+" days, "+h+" hours,"+min+" minutes, and "+sec+" seconds",
						"Conversions", JOptionPane.PLAIN_MESSAGE);	
	
			}
			

			else if(ask.equals("Money")) {
				System.out.println("Enter your money as \"Dollar.Cents\" in U.S. Dollars");
				m = reader.nextDouble();
				
				m1 = m*0.86;	//Euro
				m2 = m*0.00016;	//BitCoin
				m3 = m*19.3;	//Peso
				
				ask = "null";
				loop = "true";
				JOptionPane.showMessageDialog(null,
						"\nYour $"+m+" is equal to\n"+rnd2.format(m1)+" Euros\n"+rnd5.format(m2)+" BitCoins\n"+rnd2.format(m3)+" Pesos\n",
						"Conversion", JOptionPane.PLAIN_MESSAGE);

			}
			
			else if(ask.equals("Inches")) {
				
				System.out.println("Enter your length in Inches");
				i = reader.nextDouble();
				cm = i*2.54;
				
				ask = "null";
				loop = "true";
				JOptionPane.showMessageDialog(null,
						"\n"+i+" inches is eqaul to "+cm+" centimeters\n",
						"Conversion", JOptionPane.PLAIN_MESSAGE);	
	
			}
			
			else if(ask.equals("Seconds")) {
				
				System.out.println("Enter the number of seconds to convert");
				s = reader.nextDouble();
				
				w = (int)s/604800;
				wr = s - (int)w*604800;
				d = (int)wr/86400;
				dr = wr - (int)d*86400;
				h = (int)dr/3600;
				hr = dr - (int)h*3600;
				min = (int)hr/60;
				minr = hr - (int)min*60;
				sec = (int)minr;
				
				ask = "null";
				loop = "true";
				JOptionPane.showMessageDialog(null,
						"\n"+s+" seconds is equal to "+w+" weeks, "+d+" days, "+h+" hours,"+min+" minutes, and "+sec+" seconds\n",
						"Conversion", JOptionPane.PLAIN_MESSAGE);	
			}
			else if(ask.equals("End")) {
				loop = "false";
				reader.close();	
			}
			else if(ask.equals("null")) {
				System.out.println("Do you want to convert Money, Inches, Seconds, or All? End the program by typing \"End\".");
					ask = reader.nextLine();
			}
		}
	}
}
}





我尝试了什么:



我尝试将else if语句更改为if,else和else的每个组合和变体如果我能考虑以及更改所有if语句的标准



仅使用警告GUI的else语句而不是所有&&!ask。等于东西

也只是使用单个&



使用第三个外部变量来指导if语句的流程



更改语句的顺序(此时我非常绝望)



What I have tried:

I have tried changing the "else if" statements to every combination and variation of if, else and else if I could think of as well as changing the criteria for all of the if statements

Using just an else statement for the warning GUI not all of the &&!ask.equals stuff
Also just using a single &

Using a third, outside variable to direct the flow of the if statements

Changing the order of the statements (I was desperate at this point)

推荐答案

+ m + 等于\ n + rnd2.format(m1)+ Euros \ n + rnd5.format(m2)+ BitCoins \ n + rn d2.format(m3)+ Pesos\\\
\ n
+
i + inches is eqaul to + cm + centimeters\\\
\ n
+
s + 秒等于 + w + 周, + d + < span class =code-string> days, + h + hours, + min + 分钟和 + sec + seconds
转化,JOptionPane.PLAIN_MESSAGE);

}


else if (ask.equals( Money)){
System.out.println( 以美元输入您的钱作为\Dollar.Cents\;
m = reader.nextDouble();

m1 = m * 0 86 ; // 欧元
m2 = m * 0 00016 ; // BitCoin
m3 = m * 19 3 ; // Peso

ask = null;
loop = true;
JOptionPane.showMessageDialog(null,
\ n您的
"+m+" is equal to\n"+rnd2.format(m1)+" Euros\n"+rnd5.format(m2)+" BitCoins\n"+rnd2.format(m3)+" Pesos\n\n"+ i+" inches is eqaul to "+cm+" centimeters\n\n"+ s+" seconds is equal to "+w+" weeks, "+d+" days, "+h+" hours,"+min+" minutes, and "+sec+" seconds", "Conversions", JOptionPane.PLAIN_MESSAGE); } else if(ask.equals("Money")) { System.out.println("Enter your money as \"Dollar.Cents\" in U.S. Dollars"); m = reader.nextDouble(); m1 = m*0.86; //Euro m2 = m*0.00016; //BitCoin m3 = m*19.3; //Peso ask = "null"; loop = "true"; JOptionPane.showMessageDialog(null, "\nYour


+ m + 等于\ n + rnd2.format (m1)+ Euros \ n + rnd5.format(m2)+ BitCoins \ n + rnd2.format(m3)+ Pesos \\\

转换,JOptionPane.PLAIN_MESSAGE);

}

else if (问。 equals( 英寸)){

System.out.println( 以英寸为单位输入长度);
i = reader.nextDouble();
cm = i * 2 54 ;

ask = null;
loop = true;
JOptionPane.showMessageDialog(null,
\ n + i + < span class =code-string> inches is eqaul to + cm + centimeters \\\

转换,JOptionPane.PLAIN_MESSAGE);

}

else if (问。 equals( Seconds)){

System.out.println( 输入转换的秒数);
s = reader.nextDouble();

w =( int )s / 604800;
wr = s - ( int )w * 604800 ;
d =( int )wr / 86400;
dr = wr - ( int )d * 86400 ;
h =( int )dr / 3600;
hr = dr - ( int )h * 3600 ;
min =( int )hr / 60;
minr = hr - ( int )min * 60 ;
sec =( int )minr;

ask = null;
loop = true;
JOptionPane.showMessageDialog(null,
\ n + s + < span class =code-string> 秒等于 + w + 周, + d + days, + h + hours, + min + 分钟, + sec + seconds \ n
Conversion,JOptionPane.PLAIN_MESSAGE);
}
else if (ask.equals( 结束)){
loop = < span class =code-string> false;
reader.close();
}
else if (ask.equals( null)){
System.out.println( 是否要转换Money,Inches,Seconds或All?通过键入\End \来结束程序。);
ask = reader.nextLine();
}
}
}
}
}
"+m+" is equal to\n"+rnd2.format(m1)+" Euros\n"+rnd5.format(m2)+" BitCoins\n"+rnd2.format(m3)+" Pesos\n", "Conversion", JOptionPane.PLAIN_MESSAGE); } else if(ask.equals("Inches")) { System.out.println("Enter your length in Inches"); i = reader.nextDouble(); cm = i*2.54; ask = "null"; loop = "true"; JOptionPane.showMessageDialog(null, "\n"+i+" inches is eqaul to "+cm+" centimeters\n", "Conversion", JOptionPane.PLAIN_MESSAGE); } else if(ask.equals("Seconds")) { System.out.println("Enter the number of seconds to convert"); s = reader.nextDouble(); w = (int)s/604800; wr = s - (int)w*604800; d = (int)wr/86400; dr = wr - (int)d*86400; h = (int)dr/3600; hr = dr - (int)h*3600; min = (int)hr/60; minr = hr - (int)min*60; sec = (int)minr; ask = "null"; loop = "true"; JOptionPane.showMessageDialog(null, "\n"+s+" seconds is equal to "+w+" weeks, "+d+" days, "+h+" hours,"+min+" minutes, and "+sec+" seconds\n", "Conversion", JOptionPane.PLAIN_MESSAGE); } else if(ask.equals("End")) { loop = "false"; reader.close(); } else if(ask.equals("null")) { System.out.println("Do you want to convert Money, Inches, Seconds, or All? End the program by typing \"End\"."); ask = reader.nextLine(); } } } } }





我尝试了什么:



我尝试将else if语句更改为if,else和else的每个组合和变体如果我能考虑以及更改所有if语句的标准



仅使用警告GUI的else语句而不是所有&&!ask。等于东西

也只是使用单个&



使用第三个外部变量来指导if语句的流程



更改语句的顺序(此时我非常绝望)



What I have tried:

I have tried changing the "else if" statements to every combination and variation of if, else and else if I could think of as well as changing the criteria for all of the if statements

Using just an else statement for the warning GUI not all of the &&!ask.equals stuff
Also just using a single &

Using a third, outside variable to direct the flow of the if statements

Changing the order of the statements (I was desperate at this point)


引用:

我尝试将else if语句更改为if,else和else的每个组合和变体,如果我能想到以及更改所有if语句的标准

只使用警告GUI的else语句而不是所有&&am p。!ask.equals东西

也只是使用单个&

使用第三个外部变量来指导if语句的流程

更改语句的顺序(此时我非常绝望)

I have tried changing the "else if" statements to every combination and variation of if, else and else if I could think of as well as changing the criteria for all of the if statements
Using just an else statement for the warning GUI not all of the &&!ask.equals stuff
Also just using a single &
Using a third, outside variable to direct the flow of the if statements
Changing the order of the statements (I was desperate at this point)



尝试随机的东西不是解决方案,你需要了解你的代码在做什么,并且有一个允许您在执行和检查变量时查看代码的工具。该工具是调试器

-----

您的代码行为不符合您的预期,或者您不理解为什么!



有一个几乎通用的解决方案:一步一步地在调试器上运行你的代码,检查变量。

调试器在这里向你展示你的代码正在做什么,你的任务是与它应该做什么进行比较。

调试器中没有魔法,它不知道你的cpde应该做什么,它不要找不到错误,只是通过告诉你发生了什么来帮助你。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行,并在执行时检查变量。

调试器 - 维基百科,免费的百科全书 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows /jdb.html [ ^ ]

https: //www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]

调试器仅显示您的代码正在执行的操作,您的任务是与什么进行比较应该这样做。


Trying random things is not a solution, you need to understand what your code is doing, and there is a tool which allow you to watch your code as it perform and inspect variables. That tool is the debugger.
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your cpde is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.
Debugger - Wikipedia, the free encyclopedia[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.


这篇关于一个“如果”声明正在被触发,即使它不应该被触发,无论我如何尝试修复它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆