Java 斜边程序,can';在用户输入2之前,我不知道如何让程序运行
我正在写一个程序,找到三角形的斜边,我需要让程序运行任意次数,直到用户输入2。当用户输入2时,我不知道如何结束程序Java 斜边程序,can';在用户输入2之前,我不知道如何让程序运行,java,Java,我正在写一个程序,找到三角形的斜边,我需要让程序运行任意次数,直到用户输入2。当用户输入2时,我不知道如何结束程序 package assignment5a; import java.util.Scanner;//import Scanner public class Assignment5A { public static void main(String[] args) { Scanner sc = new Scanner(System.in);//new
package assignment5a;
import java.util.Scanner;//import Scanner
public class Assignment5A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);//new Scanner variable
int answer;
double side1, side2, result;
System.out.println("Enter 1 to calculate the hypotenuse of a triangle or enter 2 to quit.");
answer = sc.nextInt();
while(answer < 0 || answer > 2){
System.err.println("Please enter a valid answer.");
System.out.println("Enter 1 to calculate the hypotenuse of a triangle or enter 2 to quit.");
answer = sc.nextInt();
}
System.out.println("Enter side 1 of the triangle :");//input for side 1
side1 = sc.nextDouble();
System.out.println("Enter side 2 of the triangle :");//input for side 2
side2 = sc.nextDouble();
result = hypotenuse(side1, side2);//declares result as the result of the method hypotenuse
System.out.printf("Hypotenuse of your triangle is: %.2f%n", result);//prints results
}
public static double hypotenuse(double s1, double s2){//method for calculating hypotenuse
double hypot;
hypot = Math.sqrt((Math.pow(s1, 2) + Math.pow(s2, 2)));
return hypot;
}
}
包分配5a;
导入java.util.Scanner//导入扫描仪
公共类分配5A{
公共静态void main(字符串[]args){
Scanner sc=new Scanner(System.in);//new Scanner变量
int答案;
双面1,双面2,结果;
System.out.println(“输入1计算三角形的斜边,或输入2退出”);
答案=sc.nextInt();
while(答案<0 | |答案>2){
System.err.println(“请输入有效答案”);
System.out.println(“输入1计算三角形的斜边,或输入2退出”);
答案=sc.nextInt();
}
System.out.println(“输入三角形的第1面:”;//输入第1面
side1=sc.nextDouble();
System.out.println(“输入三角形的第2面:”;//输入第2面
side2=sc.nextDouble();
result=斜边(side1,side2);//将结果声明为斜边方法的结果
System.out.printf(“三角形的斜边为:%.2f%n”,result);//打印结果
}
公共静态双斜边(双s1,双s2){//斜边计算方法
双重低血压;
hypop=Math.sqrt((Math.pow(s1,2)+Math.pow(s2,2));
回压;
}
}
有几个选项:
if (answer == 2)
{
break;
}
if (answer == 2)
{
return;
}
if (answer == 2)
{
System.exit(0);
}
Wilmol的回答和Elliot Frisch的回答/评论是解决方案的一半 另一半是你需要一个围绕大部分逻辑的外部循环,这样它就会重复。将大部分
main()
放在一个循环中,该循环使用while(true){
来启动,这样它将永远循环
然后使用
if(answer==2){
..的逻辑,当用户输入2时,实际上会爆发出来。所以我就想出来了。你的答案帮助很大,但我最后放了两个while循环。代码如下:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);//new Scanner variable
int answer;
double side1, side2, result;
System.out.println("Enter 1 to calculate the hypotenuse of a triangle or enter 2 to quit.");
answer = sc.nextInt();
while(answer < 0 || answer > 2){
System.err.println("Please enter a valid answer.");
System.out.println("Enter 1 to calculate the hypotenuse of a triangle or enter 2 to quit.");
answer = sc.nextInt();
}
while(answer == 1){
System.out.println("Enter side 1 of the triangle :");//input for side 1
side1 = sc.nextDouble();
System.out.println("Enter side 2 of the triangle :");//input for side 2
side2 = sc.nextDouble();
result = hypotenuse(side1, side2);//declares result as the result of the method hypotenuse
System.out.printf("Hypotenuse of your triangle is: %.2f%n", result);//prints results
System.out.println("Enter 1 to calculate the hypotenuse of a triangle or enter 2 to quit.");
answer = sc.nextInt();
}
}
如果(答案=2)return;
在您的代码中,您认为退出决定的最佳位置是哪里?我需要它来循环main方法,它需要在用户输入1后提示用户输入三角形的边,因此在用户输入2之前它不会退出。我尝试了这个方法,但它没有正确运行。我不确定我是否做得不对。@JoshAckerman为什么不发布您尝试过的代码?目前,您的当前代码中没有试图重复您的逻辑,也没有尝试退出“2”。除非他们能够理解您正在努力解决的问题,否则没有人能够提供有用的答案。这个答案准确地解释了您需要做什么来实现所描述的预期结果-这是一个多么好的问题这一方面对你不起作用?
double hypot;
hypot = Math.sqrt((Math.pow(s1, 2) + Math.pow(s2, 2)));
return hypot;
}