[英]How can I multiply a with either b or c after I solved my initial out of bounds exception
I am new to Java programming and I want to make a program of calculating the speed of a car in relation of the gear and the RPM. 我是Java编程的新手,我想编写一个程序来计算与齿轮和RPM相关的汽车速度。
I searched different sites or topics, but couldn't find something. 我搜索了其他站点或主题,但找不到任何东西。 Maybe the answer was there, but I haven't seen it :D.
也许答案在那里,但我还没有看到:D。
A little but not complete program for example: 一些但不完整的程序,例如:
import java.util.Scanner;
public class Speed {
public static void main(String[] args) {
double Gear, MPH, RPM
double TireDia = 78;
double Pi = 3.1415;
double RatioGearOne = 3.4;
double RatioGearTwo = 2.5;
double RatioGearThree = 1.6;
Scanner input = new
Scanner (System.in);
System.out.println("What gear is it in?");
Gear = input.nextDouble();
System.out.println("How many RPM are shown?");
RPM = input.nextDouble();
MPH = Gear * RPM * Pi * TireDia * 60 / 1000000;
System.out.println("The car is going: " + MPH + " MPH");
}
}
Like I asked, I want that you can enter Gear 1 or Gear 2 or Gear 3 multiplied by PRM, Pi, etc to display me the MPH. 就像我问的那样,我希望您可以输入Gear 1或Gear 2或Gear 3乘以PRM,Pi等来显示MPH。 I read that you can do this with an array too, but i want it line that for now, cause I understand it a bit more at the time.
我读到您也可以使用数组来执行此操作,但是我现在暂时希望这样做,因为我当时对此有所了解。
I hope you can help me. 我希望你能帮助我。
UPDATE: 更新:
I got it working as I want it for now and cleaned it a bit with your suggestions. 我现在可以按需使用它了,并根据您的建议进行了整理。 Now I need to figure out how I get around the out of bounds error, but havent figured it out how, (searching for hours by now) and get the speed in a whole number.
现在,我需要弄清楚如何解决越界错误,但还没有弄清楚如何(现在搜索小时)并获得整数速度。 Thanks for help so far.
到目前为止,感谢您的帮助。
This is my current Program: 这是我当前的程序:
import java.util.Scanner;
public class geschwindigkeitAuto {
public static void main(String[] args) {
double gang, kmh, upm;
Scanner input = new Scanner (System.in);
double[] übersetzungen = {3.7, 1.95, 1.3, 1.03, 0.84, 0.68};
System.out.println("Welcher Gang ist Eingelegt? (wähle 1 bis 6): ");
gang = input.nextInt();
System.out.println("Wieviele Umdrehungen werden angezeigt? (wähle 0-6500): ");
upm = input.nextInt();
kmh = upm / übersetzungen[(int)gang -1] / 3.62 * 68 * 3.141 * 60 / 100000;
System.out.print("Die Geschwindigkeit des Autos beträgt: " + kmh + " kmh");
}
}
UPDATE 2 更新2
I have searched more and watched some videos for the OutOfBoundExcenption to avoid and I have found a solution so that I cannot input less than 1 or more than 6 to my program without failing. 我进行了更多搜索,并观看了一些有关OutOfBoundExcenption的视频,以避免出现这种情况,并且找到了解决方案,使我无法在程序中输入少于1或超过6的内容。 The only thing is now, how to tell the user to input a different number after he made a wrong choise instead of fist going through the whoule sequenz.
现在唯一的问题是,如何在选择错误后告诉用户输入一个不同的数字,而不是用拳头穿过漏斗序列。
import java.util.Scanner;
public class geschwindigkeitAuto {
public static void main(String[] args) {
double gang, kmh = 0, upm;
Scanner input = new Scanner (System.in);
double[] übersetzungen = {3.7, 1.95, 1.3, 1.03, 0.84, 0.68};
System.out.println("Welcher Gang ist Eingelegt? (wähle Gang 1 bis 6): ");
gang = input.nextInt();
System.out.println("Wieviele Umdrehungen werden angezeigt? (wähle 0-6500): ");
upm = input.nextInt();
try {
kmh = upm / übersetzungen[(int)gang -1] / 3.62 * 68 * 3.141 * 60 / 100000; }
catch(ArrayIndexOutOfBoundsException ex) {
System.out.println("Du hast einen nicht vorhandenen Gang gewählt! "
+ "Es gibt nur 6 Gänge zur Auswahl" + "\n"); }
System.out.println("Die Geschwindigkeit des Autos beträgt: " + kmh + " kmh" + "\n");
System.out.print("Noch eine Berechnung? (J/N) : ");
System.out.printf("\n");
String var= input.next();
if(var.equalsIgnoreCase("J")) {
main(null);
}
}
}
If you're adamant that you don't want to use an array then you can use a switch statement: 如果您坚决不想使用数组,则可以使用switch语句:
switch(gear) {
case 1: ratioGearOne * ...
case 2: ratioGearTwo * ...
...
}
etc etc. You'll also need a default: that's run whenever the input doesn't conform to one of the cases (eg print an error). 等。您还需要一个默认值:只要输入不符合其中一种情况(例如打印错误),该默认值便会运行。
If you were to use an array you could do it like this: 如果要使用数组,可以这样:
double[] ratios = { 3.4, 2.5, 1.6 }
Then you could calculate the MPH like so: 然后,您可以像这样计算MPH:
MPH = ratios[gear - 1] * ...
Note you need to subtract one from the gear since array indexes start from 0. I believe you'd also need to change your gear variable to an int (use nextInt()) since you can't have half an index. 请注意,由于数组索引从0开始,因此您需要从齿轮中减去1。我相信您还需要将gear变量更改为int(使用nextInt()),因为您不能拥有一半的索引。
As other commenters have said I'd strongly recommend reading up on Java conventions (particularly variable naming) before getting into any bad habits. 正如其他评论者所说的那样,我强烈建议您在养成任何不良习惯之前,先阅读一下Java约定(尤其是变量命名)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.