我已经为程序创建了基本结构,但无论我尝试什么,我似乎都无法退出循环。
此外,我希望它显示用户将输入的信息;一旦我退出循环,从键盘到控制台,但我不知道如何做到这一点。
import java.util.Scanner;
public class Requirement1 {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
String name, game, time, points;
int i = 0;
System.out.println("Please Enter Your Name");
name = scan.nextLine();
System.out.println("Players Name: " + name);
while (i < 100)
{
Scanner scan2 = new Scanner(System.in);
System.out.println("Please Enter a Game Name (If You Are Done type \"quit\")");
game = scan.nextLine();
System.out.println("Game: " + game);
Scanner scan3 = new Scanner (System.in);
System.out.println("Please Enter Your Score");
points = scan.nextLine();
System.out.println("Your Score: " + points);
Scanner scan4= new Scanner (System.in);
System.out.println("Please Enter the amount of time Spent Playing in Minutes");
time = scan.nextLine();
System.out.println("Time played: " + time);
}
}
答案 0 :(得分:5)
在循环内包含i++
。
答案 1 :(得分:4)
为了摆脱while循环,您需要使func renderNodeHieararchyForNode(node:SKNode, index:Int) {
var i = 0
var beginning = ""
while i != index {
beginning += " "
i++
if i == index {
beginning += " "
}
}
print("\(beginning)Node of type \(node.dynamicType) \(node.name != nil ? "('\(node.name!)')" : "") has zPosition = \(node.zPosition)\(node.children.count > 0 ? " and has children :" : ".")")
for (i,child) in node.children.enumerate() {
renderNodeHieararchyForNode(child, index: index+1)
if i == node.children.count-1 {
print("")
}
}
}
func renderNodeHiearchyForNode(node:SKNode) {
renderNodeHieararchyForNode(node, index: 0)
}
为false,或者添加一个显式语句来停止循环(i < 100
,break
,{{1 }或return
,具体取决于您的要求。)
如果您想在用户输入throw
并在循环后执行代码时中断循环:
System.exit
目前,"quit"
永远不会从0更改,因此System.out.println("Please Enter a Game Name (If You Are Done type \"quit\")");
game = scan.nextLine();
if (game.equals("quit")) {
break;
}
始终为真。此外,它不会被读取。除非您出于其他目的需要它,否则可以删除i
并将while循环声明更改为:
i < 100
这种情况显然永远不会是假的,所以你需要在循环中显式中断。
答案 2 :(得分:1)
另一种方法是使用$user->{'mobilephone'} = $userDat['mobilephone'];
循环,更符合您的需求:
//code to initiate the object ImageView...
ImageView myImage = (ImageView) findViewById(R.id.pic);//pic is the id of ImageView in my xml file...
try
{
// now, get the input stream
InputStream input= getAssets().open(c.getString(c.getColumnIndex("image")));//image is the name of field in my db which holds the path (ex. rice.jpg)...
// initiate image as drawable
Drawable draw = Drawable.createFromStream(input, null);
// set image to ImageView
myImage.setImageDrawable(draw);
}
catch(IOException ex)
{
return;
}
答案 3 :(得分:1)
一般来说,当我知道循环运行的确切次数时,我喜欢使用for
循环。如果我不知道循环运行了多少次,那么我想使用while
循环。
for
循环:
import java.util.Scanner;
public class Requirement1 {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
String name, game, time, points;
System.out.println("Please Enter Your Name");
name = scan.nextLine();
System.out.println("Players Name: " + name);
for (int i = 0; i < 100; i++) {
Scanner scan2 = new Scanner(System.in);
System.out.println("Please Enter a Game Name (If You Are Done type \"quit\")");
game = scan.nextLine();
if (game.equals("quit")) {
break;
}
System.out.println("Game: " + game);
Scanner scan3 = new Scanner (System.in);
System.out.println("Please Enter Your Score");
points = scan.nextLine();
System.out.println("Your Score: " + points);
Scanner scan4= new Scanner (System.in);
System.out.println("Please Enter the amount of time Spent Playing in Minutes");
time = scan.nextLine();
System.out.println("Time played: " + time);
}
}
while
循环:
import java.util.Scanner;
public class Requirement1 {
public static void main(String[] args) {
Scanner scan = new Scanner (System.in);
String name, game, time, points;
System.out.println("Please Enter Your Name");
name = scan.nextLine();
System.out.println("Players Name: " + name);
while (true) {
Scanner scan2 = new Scanner(System.in);
System.out.println("Please Enter a Game Name (If You Are Done type \"quit\")");
game = scan.nextLine();
if (game.equals("quit")) {
break;
}
System.out.println("Game: " + game);
Scanner scan3 = new Scanner (System.in);
System.out.println("Please Enter Your Score");
points = scan.nextLine();
System.out.println("Your Score: " + points);
Scanner scan4= new Scanner (System.in);
System.out.println("Please Enter the amount of time Spent Playing in Minutes");
time = scan.nextLine();
System.out.println("Time played: " + time);
}
}
答案 4 :(得分:0)
你的循环是一个简单的计数器,你可能想用for循环替换它,因为它表明循环不会因为循环中发生的某些操作而破坏:
for(int i = 0 ; i < 100 ; i++) {
// block running 100 times
}
答案 5 :(得分:0)
如果可能,请避免使用do / while支持for循环。如果游戏循环应在用户键入quit时结束,请添加break语句。
for (i = 0; i < 100; i++) {
// get user's game choice from console
if (game.equals("quit")){
break;
}
// if they didn't type quit ...
}