编辑:谢谢大家的帮助.我能够使用我在前面章节中学到的技巧和你的建议来使它工作.非常感谢!
我决定尝试通过创建简单的文本冒险来巩固我从Java学到的东西:初学者指南.我即将开始涉及类和方法的第4章.前三章讨论了if,for,while,do-while,switch,简单的键盘交互以及break/continue.
我计划在每章后回去并编辑它以使用我学到的新技能.我几乎没有触及表面,我遇到了问题.
// A basic, but hopefully, lengthy text adventure.
class TextAdventure
{
public static void main(String args[])
throws java.io.IOException
{
System.out.println("\t\t BASIC TEXT ADVENTURE");
// variables I need, attributes, classes, character name, player's choice, gold
int str = 0, inte = 0, chr = 0, con = 0, dex = 0, gold;
char charName, choice;
System.out.println("Welcome player! You are about to embark upon a quest in the form of a text adventure.");
System.out.println("You will make choices, fight monsters, …Run Code Online (Sandbox Code Playgroud) 我对Java很新,我在其中一本书中遇到过一两次这个问题.这是一个超级简单的程序,我只是不明白为什么它不起作用.我知道当你使用return时,方法中的任何东西都没有意义.那么这意味着你执行for语句或if语句是一个返回?
我在最新版本的Eclipse中使用Windows 8上的Java 8.
这是我的简单程序:
// Find the sum of 1 through 50 and the average.
class SumAndAverage
{
public static void main(String args[])
{
int sum = 0;
double average = 0;
for(int i = 1; 1 <= 50; i++)
{
sum += i;
}
// the following code is "unreachable"
average = sum / 100;
System.out.println("The sum is: " + sum);
System.out.println("The average is " + average);
}
}
Run Code Online (Sandbox Code Playgroud)