- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
import java.util.Scanner;
public class DRYeck {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); //declaration scanner
System.out.println("What is given:");
System.out.print("How many sides?"); //input the number of Sides that are given
System.out.println("");
double input = scanner.nextInt();
System.out.print("How many angles? "); //input the number of Angles that are given
System.out.println("");
double input2 = scanner.nextInt();
if((input+input2)>3){ //observes if the input is higher than 3 and errors if this is so
System.out.println("Please enter only 3 entries, you entered "+((int)(input+input2))+" eingegeben...");
}else if((input==1) && (input2==2)){
double side1 = inputDialogSide(); //input a Side
double angle1 = inputDialogAngle(); //input an Angle
double angle2 = inputDialogAngle(); //input an Angle
oneSideTwoAnglesGeneral(side1,angle1,angle2);
}else if((input==2) && (input2==1)){
System.out.println("Is the angle inclsive or exclusiv?");
String input3 = scanner.nextLine(); //input 3 for identification if the angle is inclusive or exclusive
if(input3.equals("inklusiv")){ //set variable 'inclu' to true if 'inclusive' is entered
//distinction of cases into inclusive and exclusive angle
double side1 = inputDialogSide(); //input one Side
System.out.println("");
double side2 = inputDialogSide(); //input the other Side
System.out.println("");
double angle1 = inputDialogAngle(); //input the Angle
twoSidesOneAngleIncl(side1,side2,angle1);
}else{
double side1 = inputDialogSide(); //input one Side
double side2 = inputDialogSide(); //input the other Side
double angle1 = inputDialogAngle(); //input the Angle
twoSidesOneAngleExcl(side1,side2,angle1);
}
}else if((input==3) && (input2==0)){ //condition if 3 Sides are inputed
double side1 = inputDialogSide(); //input first Side
double side2 = inputDialogSide(); //input second Side
double side3 = inputDialogSide(); //input third Side
threeSides(side1,side2,side3);
}
}
public static double inputDialogAngle(){ //subroutine for inputing an Angle
Scanner scanner = new Scanner(System.in);
System.out.print("Enter an angle: ");
double input5 = scanner.nextDouble(); //input5(variable) for an Angle
System.out.println("");
return input5;
}
public static double inputDialogSide(){ //subroutine for inputing a Side
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the sidelength: ");
double input4 = scanner.nextDouble(); //input4(variable) for a Side
System.out.println("");
return input4;
}
static void collection(double alpha,double beta, double gamma,double sidea,double sideb,double sidec){ //sorting the value in the array
double[][] SidesAngles = new double [3][2]; //array for side length value of the angles
for(int i=0;i<SidesAngles.length;i++){
for(int j=0;j<SidesAngles[i].length;j++){
if((SidesAngles[0][0]==0)){
SidesAngles[0][0]=sidea;
}else if((SidesAngles[0][1]==0)){
SidesAngles[0][1]=alpha;
}else if((SidesAngles[1][0]==0)){
SidesAngles[1][0]=sideb;
}else if((SidesAngles[1][1]==0)){
SidesAngles[1][1]=beta;
}else if((SidesAngles[2][0]==0)){
SidesAngles[2][0]=sidec;
}else if((SidesAngles[2][1]==0)){
SidesAngles[2][1]=gamma;
}
}
}
PrintArray(SidesAngles);
}
public static double angleDiff(double angle1, double angle2){
double resuAng = 180-angle1-angle2;
return resuAng;
}
static void PrintArray(double SidesAngles[][]){ //printing the Array with all values
String[][] AngleSideNames = {{"Seite a","Alpha"},{"Seite b","Beta"},{"Seite c","Gamma"}}; //Names of angles and Sides
for(int i=0;i<SidesAngles.length;i++){
for(int j=0;j<SidesAngles[i].length;j++){
System.out.print(AngleSideNames[i][j]+": "+SidesAngles[i][j]+"\n");
}
}
}
public static double sinusRule(double angle1, double angle2, double side){
double angle3 = angleDiff(angle1,angle2);
double resuSide = side*(Math.sin(angle1)/Math.sin(angle2));
return resuSide;
}
static void threeSides(double sidea, double sideb, double sidec){ //subroutine for 3 Sides that are given
double alpha = Math.acos((Math.pow(sidea, 2)+Math.pow(sideb, 2)-Math.pow(sidec, 2))/(2*sidea*sideb));
alpha = Math.toDegrees(alpha); //alpha in degree
double beta = Math.acos((Math.pow(sidea,2)+Math.pow(sidec,2)-Math.pow(sideb,2))/(2*sidea*sidec));
beta = Math.toDegrees(beta); //output in degree
double gamma = angleDiff(beta,alpha); //determine the angle differance
collection(alpha,beta,gamma,sidea,sideb,sidec);
}
static void twoSidesOneAngleIncl(double sidea, double sideb, double gamma){ //subroutine for 1 Angle(inclusive) and 2 Sides
double sidec = Math.sqrt(Math.pow(sidea,2)+Math.pow(sideb,2)-(2*sidea*sideb*Math.cos(gamma)));
double alpha = Math.acos((Math.pow(sideb,2)+Math.pow(sidec, 2)-Math.pow(sidea, 2))/(2*sideb*sidec));
double beta = angleDiff(alpha,gamma);
collection(alpha,beta,gamma,sidea,sideb,sidec);
}
static void twoSidesOneAngleExcl(double sideb, double sidec, double beta){ //subroutine for 1 Angle(exclusive) and 2 Sides
double sidea=0,alpha=0,gamma=0; //initialize sidea, alpha, gamma
double d = ((sidec/sideb)*Math.sin(beta)); //variable for further movement
boolean acute=false,obtuse=false; //initialize check-variable for acute or obtuse triangle
if(d>1){ //condition if check-variable is higher than 1
System.out.println("There is no solution for this triangle");
}else if(d==1){ //if check-variable equals 1
gamma = 90; //gamma=90° because of the conditions
sidea = Math.sqrt(Math.pow(sideb,2)+Math.pow(sidec,2)); //solve sidea with pythagorean theorem
}else{ //if check-variable is lower than
System.out.print("Is the traingle obtuse or actuse: ");
Scanner scanner = new Scanner(System.in); //input for acute or obtuse statement
String input = scanner.nextLine();
if(input.equals("obtuse")){ //sets the right boolean to true so further operations are correct
acute = true;
}else if(input.equals("acute")){
obtuse = true;
}
if((sideb<sidec) && (acute)){ //checks if b<c and the triangle should be acute
gamma = Math.asin(d);
System.out.println("angle gamma: "+gamma);
sidea = sinusRule(beta,gamma,sideb); //Side a
}else if((sideb<sidec) && (obtuse)){ //checks if b<c and the triangle should be obtuse
gamma = Math.asin(d);
gamma = 180-gamma;
System.out.println("angle gamma: "+gamma);
sidea = sinusRule(beta,gamma,sideb); //Side a
}
System.out.println("There is no solution!!!");
}
collection(alpha,beta,gamma,sidea,sideb,sidec);
}
static void oneSideTwoAnglesGeneral(double sidec, double alpha, double beta){//subroutine for one side and two angles that are given
double gamma = angleDiff(alpha,beta); //determine the third angle
double sidea = sidec*(Math.sin(alpha)/Math.sin(gamma)); //determine side with the law of sinus
sinusRule(alpha,gamma,sidec);
double sideb = sidec*(Math.sin(beta)/Math.sin(gamma)); //same here to determine side b
sinusRule(beta,gamma,sidec);
collection(alpha,beta,gamma,sidea,sideb,sidec);
}
}
异常(exception):
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at DRYeck.inputDialogSide(DRYeck.java:85)
at DRYeck.main(DRYeck.java:52)`
我现在的问题是如何摆脱异常。该程序旨在仅使用 3 个输入来计算三角形的所有边和角。例如一个角和 2 个边或 2 个角和 1 个边等。
最佳答案
您的问题与代码的这一部分有关:
else if((input==2) && (input2==1)){
System.out.println("Is the angle inklsiv or exclusiv?");
String input3 = scanner.nextLine(); //input 3 for identification if the angle is inclusive or exclusive
if(input3.equals("inklusiv")){
由于您在此处请求的不是完整行,而只是一个标记,因此请使用 Scanner.next() 而不是 Scanner.nextLine();
public String next() Finds and returns the next complete token from this scanner. A complete token is preceded and followed by input that matches the delimiter pattern. This method may block while waiting for input to scan, even if a previous invocation of hasNext() returned true.
所以改变这一行:
String input3 = scanner.nextLine();
到
String input3 = scanner.next();
你的程序应该可以正常工作!
关于java - 在运行时获取 InputMismatchException,但编译正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30680502/
这个问题已经有答案了: Scanner double value - InputMismatchException (2 个回答) 已关闭2 年前。 为什么 Java 在使用 Scanner.next
我正在实现一个驱动程序来测试决策树,但遇到输入不匹配异常。我可以做什么来解决这个问题? 这是我的代码: import java.io.FileNotFoundException; /** * Loa
static long arrayMaxMin[]; public static void main(String[] args) { try{ Scanner in = new
这个问题在这里已经有了答案: Scanner is skipping nextLine() after using next() or nextFoo()? (23 个回答) 5年前关闭。 impor
简介 我在捕获 InputMismatchException 时遇到问题。当我搜索 InputMismatchException 时,我已经阅读了该网站上出现在第一页上的所有线程。我在阅读这些帖子时找
while(inputbook.hasNext()){ id = inputbook.nextInt(); name = inputbook.next(); year = in
我有这个代码: import java.util.Scanner; public class Maggiore3Valori { public static void main(String[
public static void main(String[] args) { try { Scanner scan = new Scanner(new Fi
当输入 55.5 作为第一个提示的答案时,我收到以下错误。输入 50 时我没有收到错误消息。 Exception in thread "main" java.util.InputMismatchExc
为什么我遇到了 inputMismatchException?当输入仅为整数时,结果会很好,但是当我使用双输入时,编译器会抛出 inputMismatchException。 public stati
我有一个任务: 5-8)(求最高分)编写一个程序,提示用户输入学生人数以及每个学生的姓名和分数,最后显示分数最高的学生的名字。 当我运行此代码并输入“john”等名称时,它运行良好,但当我尝试输入“J
我尝试以最简单的方式使用扫描仪: 代码: double gas, efficiency, distance, cost; Scanner scanner = new Scanner(System.in
我试图提示用户输入 x 坐标,但是当我输入十进制数(例如 2.1)作为输入时,这会导致问题。如何解决该问题? import java.util.Scanner; public cl
所以我一直在做一项任务,要求我们建立一个“患者经理”,根据紧急级别确定首先治疗的患者。我遇到的主要问题是,当用户在紧急栏中输入数字以外的内容时,它会使程序崩溃并给我一个 InputMismatchEx
public static void main(String[] args) throws FileNotFoundException { File file = new File("data
我写了下面这段Java代码。当运行它并输入任何值(定义的值,例如 latte,或任何其他值,例如 az 整数)时,我得到一个 InputMismatchException。 据我所知,此异常意味着输入
我希望用户输入一行文本,只要 x 不等于 numOfContestans 的值。当我运行代码时,我收到一个 InputMismatchException。有人知道如何解决这个错误吗? try {
这个问题已经有答案了: Scanner is skipping nextLine() after using next() or nextFoo()? (25 个回答) 已关闭 7 年前。 我有文本文
我无法找到错误的根源。我所做的就是从文件中读取文本 public static void main(String[] args) throws Exception { int T; S
我在将非整数输入整数字段时遇到问题。我只是采取预防措施,这样如果其他人使用/处理我的程序,他们就不会得到此 InputMismatchException。 当我在 input 变量中输入非数字字符时,
我是一名优秀的程序员,十分优秀!