Tag: switch statement

在数组中输入数据并显示null时出现程序错误

我在这里引用了一些修正,但仍然完全满足我的程序,但仍然没有完成。 问题1 =我的displayReg()出现“nullnullnullnull …”,我应该使用arraylist吗? 问题2 =我需要选择通过IC和NAME搜索searchReg()我该怎么做? 问题3 =需要在.txt文件上显示displayReg。 import java.util.*; public class RegisterMenu { private Driver[] newOwner; private final int MAX_ITEMS = 10; private int size = 0; public Driver newReg(){ Driver owner = new Driver(); Scanner scan = new Scanner(System.in); owner.setRegNo(size+1); System.out.print(“Enter Name: “); owner.setName(scan.nextLine()); System.out.print(“Enter IC: “); owner.setIc(scan.nextLine()); System.out.print(“Enter PlateNo: “); owner.carInfo.setPlateNum(scan.nextLine()); System.out.print(“Enter […]

Java,关于交换机和案例的问题?

因此,我希望在60%的时间内执行某项操作,并在40%的时间内执行另一项操作。 而且有时它也没有。 我能想到的最好的方法是通过开关和制作一堆案例。 下面是一个例子,如果这对你没有任何意义。 我的问题是,有更好的方法吗? 有没有办法让案例0-5在一个语句中执行操作1? Random rand = new Random(50); switch(rand.nextInt()) { case 1: { do action 1 } break; case 2: { do action 1 } break; case 3: { do action 1 } break; case 4: { do action 1 } break; case 5: { do action 1 } break; case 6: […]

导致java:240的switch语句(可能尚未初始化)

我一直在寻找一个答案,但是没有发现任何真正有助于我的情况如此暴露的事情: 我试图根据输入在变量中存储一个值: switch(pepperoni) { case ‘Y’: case ‘y’: topping1 = 1; break; case ‘N’: case ‘n’: topping1 = 0; break; default: { System.out.print(“This is not a valid response, please try again \n”); System.out.print(“Do you want Pepperoni? (Y/N): “); pepperoni = scan.next().charAt(0); break; } 我希望变量topping1在输入为’Y’或’y’时存储值1,如果输入为’N’或’n’则存储值0 如果输入既不是’Y’,’y’,’N’也不是’n’,那么我希望它重复该问题,直到输入有效输入为止。 当我稍后在程序中尝试打印值’因为它可能尚未初始化’时出现问题,这有点有意义。 (以下示例) if(topping1 > 0) System.out.println(“Pepperoni”); // 243: error: variable […]

OnClick侦听器未按预期运行

我在TextView ( mReadAgainButton )上设置了mReadAgainButton ,它应该从头开始重新加载一组指令(最初通过名为mAssistUpdateButton的Button上的mAssistUpdateButton )但是当点击TextView mReadAgainButton似乎没有任何事情发生。 public void onClick(View v) { if (v == mAssistUpdateButton) { // Update button for ICS and up is selected // Get the TextView in the Assist Update UI TextView tv = (TextView) findViewById(R.id.apn_app_text_cta2); String text = “”; CharSequence styledText = text; switch (mInstructionNumber) { case 0: // Retrieve […]

切换不兼容的类型错误

我正在为我的class级写一个罗马数字程序。 我正在使用switch语句将字符串转换为整数。 但是,当我运行它时,我得到一个不兼容的类型错误。 我正在运行java 7,所以这不是问题。 这是我的代码: public static void main() { // Declare local variables Scanner input = new Scanner(System.in); String rNum; int i; int[] rArray; // Display program purpose System.out.println(“This Program Converts Roman numerals to decimals”); System.out.println(“The Roman numerals are I (1), V (5), X (10), L (50), C (100), D (500) and M […]

非静态方法不能从静态上下文中引用

我的class级看起来像这样: public class Month { private int numOfMonth; private int monthNum; public int monthNum() { return monthNum = 1; } public void setMonthNum(int monthNum){ switch (monthNum) { case 1: System.out.println(“January”); break; case 2: System.out.println(“February”);break; case 3: System.out.println(“March”);break; case 4: System.out.println(“April”);break; case 5: System.out.println(“May”);break; case 6: System.out.println(“June”);break; case 7: System.out.println(“July”);break; case 8: System.out.println(“August”);break; case 9: System.out.println(“September”);break; […]

selenium web driver – 切换到父窗口

我使用selenium Web Driver。 我打开了一个父窗口。 点击链接后,新窗口打开。 我从列表中选择一些值,此窗口会自动关闭。 现在我需要在父窗口中操作。 我怎样才能做到这一点? 我尝试了以下代码: String HandleBefore = driver.getWindowHandle(); driver.findElement(By.xpath(“…”)).click(); for (String Handle : driver.getWindowHandles()) { driver.switchTo().window(Handle);} driver.findElement(By.linkText(“…”)).click(); driver.switchTo().window(HandleBefore); 但这不起作用。

在Java中切换案例逻辑表达式语句 – 与JS或PHP

在JavaScript中我也在PHP中看到它:你可以在你的案例中使用逻辑表达式:例如: switch(true){ case (d10): document.write(“Greater than 10”); break; default: document.write(“Some dumb error. Probably not a number.”); } 我想知道我们是否可以用Java做到这一点…说我需要比较一个非常大的范围。 case 1 : case 2 : … case 5000: case 5001: default: 这样做会很繁琐。 我可以在if语句中包含我的案例,但我想知道它可以像在JavaScript或PHP中那样完成。

在Java中使用switch的问题

我无法弄清楚为什么它总是返回arg1的值。 我正在建造一个重量转换器。 public double convert(double arg1,int arg2,int arg3) { // arg1 = amount, arg2 = from, arg3 = to double milligram = 1; double gram = 1000; double ounce = 28349.5; double pound = 453592; double answer = 0; switch(arg2) { case 0: switch(arg3) { // if milligram case 0: answer = (arg1 * milligram) […]

无法在java中获取Switch案例的行为

我在java 6中编写了一些小代码 public class TestSwitch{ public static void main(String… args){ int a = 1; System.out.println(“start”); switch(a){ case 1:{ System.out.println(1); case 3: System.out.println(3); case 4: System.out.println(4); } case 2:{ System.out.println(2); case 5: System.out.println(5); case 7: System.out.println(7); } } System.out.println(“end”); } } 输出:开始1 2结束 我的编辑器显示“案例3”和“案例5”的孤立案例 。它仍在运行并显示输出。 Java中存在类似概念的Nastated案例吗? 为什么它提供以上输出? 而我认为这将是’开始1结束’ 非常感谢您的回复!!