gpt4 book ai didi

java - 我的 if 语句有问题

转载 作者:行者123 更新时间:2023-11-30 06:26:02 25 4
gpt4 key购买 nike

我在 AP 计算机科学课上陷入了困境,几乎不知道发生了什么。我一直在codingbat.com上练习,但我仍然需要一些帮助来解决一些错误:

Syntax error on token “boolean", @ expected.

和:

Syntax error on tokens, ClassHeader expected instead.

编辑:好的,我做了一些工作,然后想出了这个

public class sleepIn 
{
public static void main (String [] args)
{
boolean weekday=true;
boolean vacation=true;

if(weekday==true && vacation==false)
{
return false;
}
if(weekday==false && vacation==true)
{
return true;
}
}
}

public boolean sleepIn(boolean weekday, boolean vacation)
{
public static void main (String [] args)
{
boolean weekday=true;
boolean vacation=true;

if(weekday==true && vacation==false)
{
return false;
}
}

}

I'm still getting errors on it, but now they are different. They are:

File: C:\Users\ralph\Desktop\sleepIn.java [line: 10] Error: Void methods cannot return a value

File: C:\Users\ralph\Desktop\sleepIn.java [line: 14] Error: Void methods cannot return a value

最佳答案

您似乎已在另一个函数中声明了程序的主函数。如果删除

public static void main (String[] args){}

从函数中它可以工作。 java 程序将需要 main 函数,但仅在类中定义,而不是其他函数。

//这是我的编辑,用于解释您应该如何编写此程序

public class MyClass {
//I define the class here^^

//Public function
public static void main(String args[]) {

//Here in the main function I will call the sleepIn

boolean sleepin = sleepIn(true,true);

//I am setting weekday and vacation to true, it should in return print true
System.out.println(sleepin); //It prints TRUE


}

//Your sleepIn function
public static boolean sleepIn(boolean weekday, boolean vacation){
if (weekday == true && vacation == true){

//Here the function returns true (boolean)

return true;
}

//The function returns false (boolean)

return false;
}
}

因此,为了解释发生的情况,我定义了一个类,然后在该类中我有 main 方法(所有代码将在其中执行),并且我有 sleepIn 函数(根据输入返回 true 或 false)。正如您所看到的,当我在 main 方法中将两个 praram 设置为 true 来调用 sleepIn 函数时,它会按预期返回一个 boolean 值 (false)。然后我打印结果。

关于java - 我的 if 语句有问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47191591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com