小编Sta*_*wed的帖子

如何正确使用goto语句

我正在上高中AP计算机科学课.

我决定向goto我们的一个实验室发表声明,只是为了玩,但我得到了这个错误.

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    Syntax error on token "goto", assert expected
    restart cannot be resolved to a variable
at Chapter_3.Lab03_Chapter3.Factorial.main(Factorial.java:28)
Run Code Online (Sandbox Code Playgroud)

goto在Stackoverflow上找到了一个问题,以了解如何正确地完成它,并且我完全按照其中一个答案进行了演示.我真的不明白为什么编译器需要一个assert语句(至少这是我想的它),我也不知道如何使用assert.它似乎希望重启部分goto restart;是一个变量,但重启只是一个标签,将程序拉回到第10行,以便用户可以输入有效的int.如果它想重新启动变量,我该怎么做?

import java.util.*;

public class Factorial 
{
    public static void main(String[] args) 
    {
        int x = 1;
        int factValue = 1;
        Scanner userInput = new Scanner(System.in);
        restart:
        System.out.println("Please enter a nonzero, nonnegative value to be factorialized.");
        int factInput = userInput.nextInt();

        while(factInput<=0) …
Run Code Online (Sandbox Code Playgroud)

java loops goto

27
推荐指数
4
解决办法
19万
查看次数

同意Xcode/iOS许可证需要管理员权限,请通过sudo重新运行root操作系统更新后无法运行git

IDE正在给出一条错误消息

can't start git: /usr/bin/git但是路径是正确的,它之前是有效的.idea.log包含以下错误:

Agreeing to the Xcode/iOS license requires admin privileges, please re-run as root via sudo.
Run Code Online (Sandbox Code Playgroud)

java xcode homebrew intellij-idea macos-sierra

14
推荐指数
1
解决办法
6385
查看次数

理解try catch最后返回它返回的值和值

我有以下代码.

public static void main(String[] args) {
    System.out.println(returnString());
}
private static String returnString(){
    try {
        System.out.println("Executing try");
        return "Return try value";
    } catch (Exception e){
        System.out.println("Executing Catch");
        return "Return catch value";
    } finally {
        System.out.println("Executing finally");
        return "Return finally value";
    }
}
Run Code Online (Sandbox Code Playgroud)

这个输出是

Executing try
Executing finally
Return finally value
Run Code Online (Sandbox Code Playgroud)

如果我改变我的finally块而不返回任何类似的东西

public static void main(String[] args) {
    System.out.println(returnString());
}
private static String returnString(){
    try {
        System.out.println("Executing try");
        return "Return try value";
    } catch (Exception e){
        System.out.println("Executing Catch");
        return "Return catch …
Run Code Online (Sandbox Code Playgroud)

java return return-value try-catch-finally

8
推荐指数
1
解决办法
1978
查看次数

测试返回类型为 void 的方法

我是软件测试的新手。在一个教程中,我读到我可以测试一些这样的方法:

@Test
 public void testMultiply() {
   MyClass tester = new MyClass();
   assertEquals("10 x 5 must be 50", 50, tester.multiply(10, 5));
 } 
Run Code Online (Sandbox Code Playgroud)

所以我必须将一些参数传递给一个方法并获得一个返回值。
但就我而言,我有一些没有任何参数和返回值的类:

ArrayList al = new ArrayList();

public void Main(){
  methodA();
  methodB();
}

public void methodA(){
  // connect to URL
  // get some data
  for(int i=0; i<someThing; i++){
    al.add(data);
  }
}
public void methodB(){
  // do something with al
}
Run Code Online (Sandbox Code Playgroud)

是否也可以测试它们?例如,正确的值是否来自methodA().

java testing junit mockito

6
推荐指数
1
解决办法
3万
查看次数

编译器理解 - 在if语句中赋值

我最近遇到了这种语法

int i;
String s = "test";
if(!((i=s.length()) == 0)) {
    System.out.println(i);
}
Run Code Online (Sandbox Code Playgroud)

如果你想知道它打印什么打印:

4
Run Code Online (Sandbox Code Playgroud)

现在我知道这个代码编译以及正确运行.我也明白这会将s.length的值放入i中.我想知道这怎么可能?我们如何在if子句中等同/赋值变量.是否有人有任何见解,不知道如何使用编译器

java if-statement assign

2
推荐指数
1
解决办法
131
查看次数

java中如何返回到特定的代码行

好的,这是我在这里的第一篇文章。(善待我 :)) 我就像 Java 两周大,想创建一个应用程序,可以完成总分、平均分和平均分的工作。我的问题是。一个操作结束后如何返回到特定的代码行 例如:我希望代码运行String menu1="Input the number of the operation you want"; 在找到总数或平均值之后。提前致谢

import java.util.Scanner;
public class grader
{
    public static void main (String args[])
    {
    Scanner input=new Scanner(System.in);
    System.out.println ("Input your Chemistry marks");
    float a= input.nextFloat();

    System.out.println ("Input your Biology marks");
    float b= input.nextFloat();

    System.out.println ("Input your Physics marks");
    float c= input.nextFloat();

    String menu1="Input the number of the operation you desire ";
    String op1="1. Total ";
    String op2="2. Average ";
    String op3="3. Grade ";
    String op4="4. Exit …
Run Code Online (Sandbox Code Playgroud)

java repeat

1
推荐指数
1
解决办法
2万
查看次数