java中如何统计输入的次数
public class Text
{
public ststic void main(String[] args)
{
String ansname
int i
int num =0
for(i=1;i<=32;i++)
{
System.out.println("Enter a competitor's name( type 'end' to finish):");
ansname = input.next();
if ("end".equalsIgnoreCase(ansname))
{
break;
}
}
num=?
}
}
num为输入的次数,怎么显示输入次数
------解决方案--------------------package com.cslg.xuxing.cn;
public class Demo02 {
public static void main(String[] args) {
String ansname;
int i;
int num =0;
for(i=1;i <=32;i++)
{
System.out.println("Enter a competitor's name( type 'end' to finish):");
ansname = input.next();
num++;
if("end".equalsIgnoreCase(ansname)) {
break;
}
}
Systen.out.println("输入次数"+num);
}
}
ansname = input.next(); 这个我不是太明白,你能不能说明一下呀!
------解决方案--------------------import java.util.*;
public class Text {
public static int Print(String name) {
int num = 0;
int n = name.indexOf(" ");
while (n != -1) {
name = name.trim();
n = name.indexOf(" ");
if (n != -1 && name.substring(0, n).length() > 0)
num++;
if (n != -1)
name = name.substring(n, name.length());
}
return num + 1;
}
public static void main(String[] args) {
Scanner Jie = new Scanner(System.in);
System.out
.println("Enter a competitor's name( type 'Enter' to finish):");
String name = Jie.nextLine();
System.out.println(Print(name));
}
}
不知道楼主是怎么想的,for(i=1;i <=32;i++)
if ("end".equalsIgnoreCase(ansname))
{
break;
看不懂。
------解决方案--------------------Java code
public static void main(String[] args) {
String ansname;
int i;
int num = 0;
Scanner input = new Scanner(System.in);//获得输入
for (i = 1; i <= 32; i++) {
System.out
.println("Enter a competitor's name( type 'end' to finish):");
ansname = input.next();//取得下一行
if ("end".equalsIgnoreCase(ansname)) {
break;
}
num++;
}
System.out.println("你输入" + num + "次");//输出次数
}