이것은 내 첫 번째 게시물이므로 오해가 생겨서 유감스럽게 생각합니다.이 계산기를 C++로 작성 했으므로 훌륭하게 작동하여 java로 만들었습니다. 나는이 프로그램을 실행하면 그것은 나에게 오류지만을 제공하지 내 내 while 루프 내부의 문은 당신이 원하는 것을하지 않는 ==
을 사용하여 Java에서 문자열을 비교if 문이 while 루프 내에서 작동하지 않을 경우
import java.util.Scanner;
public class ohmlaw
{
public static void main(String args[])
{
float current;
float resistance;
float voltage;
String calchoice = new String();
Scanner cc = new Scanner(System.in);
System.out.println("OHMCAL, OHM'S LAW CALCULATOR BY RASHAAD BRYAN");
System.out.println();
System.out.println();
System.out.println();
System.out.println("Instructions");
System.out.println("If you want to calculate the voltage type voltage");
System.out.println("If you want to calculate the current type current");
System.out.println("If you want to calculate the resistance type resistance");
System.out.println("If you want to stop the program type stop");
System.out.println();
System.out.println();
System.out.println();
System.out.print("Please enter calculation choice ");
calchoice = cc.nextLine();
System.out.println();
while (calchoice != "stop")
{
if (calchoice == "current")
{
System.out.print("Please enter voltage (V) ");
voltage = cc.nextFloat();
System.out.println();
System.out.print("Please enter resistance() ");
resistance = cc.nextFloat();
System.out.println();
current = voltage/resistance;
System.out.println("The current is = " + current + "A");
System.out.println();
}
if (calchoice == "voltage")
{
System.out.print("Please enter current (V) ");
current = cc.nextFloat();
System.out.println();
System.out.print("Please enter resistance() ");
resistance = cc.nextFloat();
System.out.println();
voltage = current * resistance;
System.out.println("The voltage is = " + voltage + "V");
System.out.println();
}
if (calchoice == "resistance")
{
System.out.print("Please enter Voltage (V) ");
voltage = cc.nextFloat();
System.out.println();
System.out.print("Please enter current (I) ");
current = cc.nextFloat();
System.out.println();
resistance = voltage/current;
System.out.println("The resistance is = " + resistance + "");
System.out.println();
}
System.out.print("Please enter calculation choice ");
calchoice = cc.nextLine();
System.out.println();
System.out.println();
}
System.out.println("Thank you for using OHMCAL, have a nice day :D");
System.out.println();
}
}
==
의를 사용합니다. 문자열 비교에 equals 메서드를 사용합니다. – phoenix