我正在尝试制作一种方法,告诉我天气与否是一个数字是素数的真假.这是代码:
class prime
{
public static boolean prime (int a, int b)
{
if (a == 0)
{
return false;
}
else if (a%(b-1) == 0)
{
return false;
}
else if (b>1)
{
prime (a, b-1) ;
}
else
{
return true;
}
}
public static void main (String[] arg)
{
System.out.println (prime (45, 45)) ;
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译这个时,我收到此错误消息:
prime.java:23: missing return statement
}
^
1 error
Run Code Online (Sandbox Code Playgroud)
我可能会错误地解释错误消息的内容,但在我看来,由于我对每个可能的条件都有一个return语句,因此没有丢失的return语句.如果a为0然后它返回false,如果不是那么它检查a是否可被b分割,如果是,那么它返回如果不是那么如果b大于1则重新开始.如果b不大于1,它也会返回.
此外,让这个方法采用两个相同int的整数似乎有点混乱.
我的语法有什么问题/为什么我收到错误信息?有没有办法使它,以便我在main中使用的方法只需要一个int(也许另一个方法将该int拆分为两个克隆然后传递给public static boolean prime正确的?
还是有一种更有效的解决方法,我完全错过了?
这是示例代码:
public static void col (int n)
{
if (n % 2 == 0)
n = n/2 ;
if (n % 2 != 0)
n = ((n*3)+1) ;
System.out.println (n) ;
if (n != 1)
col (n) ;
}
Run Code Online (Sandbox Code Playgroud)
这个工作正常,直到它下降到2.然后它2 4 2 4 2 4 2 4 2 4无限输出.在我看来,如果输入2为n则为2,则将2 (n % 2 == 0)除以2,然后将打印1,因为(n != 1)为假,循环将终止.
为什么不这样呢?
这是方法:
public static String CPUcolor ()
{
System.out.println ("What color am I?") ;
String s = getIns() ;
System.out.println ("are you sure I'm "+s+"? (Y/N)") ;
String a = getIns() ;
while (!((a.equals ("y")) || (a.equals ("Y")) || (a.equals ("n")) || (a.equals ("N"))))
{
System.out.println ("try again") ;
a = getIns () ;
}
if (a.equals ("n") || a.equals("N"))
{CPUcolor() ;}
System.out.println ("I am "+s) ;
return s ;
}
Run Code Online (Sandbox Code Playgroud)
这是此方法的可能输出(y和n是用户输入):
Run Code Online (Sandbox Code Playgroud)What color am I? red are you sure I'm …
我被告知过
int[] numbers
Run Code Online (Sandbox Code Playgroud)
和
int numbers[]
Run Code Online (Sandbox Code Playgroud)
是等价的.我只见过前者.如果有的话,写下后者有什么动力?
这是代码:
class testsum
{
public static void main(String arg[])
{
double sum=0;
double fraction;
fraction=-1/9;
System.out.println("fraction: "+fraction);
fraction=-1;
fraction=fraction/9;
System.out.println("fraction: "+fraction);
}
}
Run Code Online (Sandbox Code Playgroud)
输出0然后是-0.11111111
为什么是第一个输出0而不是-0.11111111111?
我创建了一个java程序来计算无穷大:
class up {
public static void up (int n) {
System.out.println (n) ;
up (n+1) ;
}
public static void main (String[] arg) {
up (1) ;
}
}
Run Code Online (Sandbox Code Playgroud)
我实际上没想到它会到达那里,但我发现有点好奇的是它每次停在相同的数字:518669
这个号码有什么意义?(或者我想这个数字+1).
这就是我试图这样做的方式:
interface a{}
class b implements a{
a[] array;
new b(){
array={ new aImplementer(), new aImplementer(), new aImplementer()};
}
}
Run Code Online (Sandbox Code Playgroud)
为什么我不能这样做?我只是做错了吗?现在,我得到的错误是{array = {的一部分表达错误的非法开始
#include <stdlib.h>
#include <stdio.h>
main()
{
const char* str_int = "777";
const char* str_float = "333.3";
int i = atoi(str_int);
float f = atof(str_float);
printf("%s %s", i, f);
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试了一些我在网上找到的示例代码,它们都导致了总线错误。为什么会这样?
我System.out.prinln不时想用来调试东西而不是使用调试器,或者我想要一个简单的程序写入标准输出,这样我就可以记录一些东西而不花时间来设置正确的日志.我注意到有时候我的文字会不按顺序打印出来.例如:
System.out.println("A");
System.out.println("B");
System.out.println("C");
Run Code Online (Sandbox Code Playgroud)
可能导致
A
C
B
Run Code Online (Sandbox Code Playgroud)
正在印刷.
我很确定我不是疯了,所以我有两个问题:
编辑:更多信息:
我正在运行使用JUnit构建Lucene查询的单元测试.为了打印出来,我写了这堂课:
public class LogHelper { //TODO-DAVID remove
public static final boolean ENABLED = true;
public static final boolean DISABLED = false;
private boolean enabled;
public LogHelper(boolean enabled){
this.enabled = enabled;
}
public synchronized void debug(String someString){
if(enabled){
System.out.println(someString);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试将'debug()'同步,以防多个线程调用它,但奇怪的打印偶尔也会发生故障.