Java 如果变量结果超过两个,如何显示逗号;及;结果如何?
由用户(x,y)给出给定数字中5的倍数的代码。如果没有显示,则打印“无”。如果有两个要显示,请用“和”分隔。如果有两个或多个要显示,请在其末尾用逗号和“And”分隔Java 如果变量结果超过两个,如何显示逗号;及;结果如何?,java,string,delimiter,delimiter-separated-values,Java,String,Delimiter,Delimiter Separated Values,由用户(x,y)给出给定数字中5的倍数的代码。如果没有显示,则打印“无”。如果有两个要显示,请用“和”分隔。如果有两个或多个要显示,请在其末尾用逗号和“And”分隔 System.out.print("Enter first number: "); int x = new Scanner (System.in).nextInt(); System.out.print("Enter last number: "); int y = new Scanner (System.in).nextInt()
System.out.print("Enter first number: ");
int x = new Scanner (System.in).nextInt();
System.out.print("Enter last number: ");
int y = new Scanner (System.in).nextInt();
System.out.print("The multiples of 5 from "+x+ " to " +y+ " : ");
for (;x<=y; x++) {
if(x%5==0) {
System.out.printf("%,d ",x);
}
}
样本输出:
Enter number: 1
Enter number: 4
The multiples of 5 from 1 to 4: NONE
Enter number: 8
Enter number: 12
The multiples of 5 from 8 to 12: 10
Enter number: 1
Enter number: 17
The multiples of 5 from 1 to 17: 5, 10, and 15.
样本输出:
Enter number: 1
Enter number: 4
The multiples of 5 from 1 to 4: NONE
Enter number: 8
Enter number: 12
The multiples of 5 from 8 to 12: 10
Enter number: 1
Enter number: 17
The multiples of 5 from 1 to 17: 5, 10, and 15.
编辑:我编辑了我的答案以匹配结果数组列表的所有大小:
class Main {
public static void main(String[] args) {
System.out.print("Enter first number: ");
int x = new Scanner(System.in).nextInt();
System.out.print("Enter last number: ");
int y = new Scanner(System.in).nextInt();
System.out.print("The multiples of 5 from " + x + " to " + y + " : ");
ArrayList<Integer> results = new ArrayList<Integer>();
for (; x <= y; x++) {
if (x % 5 == 0) {
results.add(new Integer(x));
}
}
if (results.size() == 0) {
System.out.println("No results to display.");
} else if (results.size() == 1) {
System.out.println(results.get(0));
} else {
for (int i = 0; i < results.size() - 2; i++) {
System.out.print(results.get(i) + ", ");
}
System.out.print(results.get(results.size() - 2));
System.out.println(" and " + results.get(results.size() - 1));
}
}
}
主类{
公共静态void main(字符串[]args){
系统输出打印(“输入第一个数字:”);
int x=新扫描仪(System.in).nextInt();
系统输出打印(“输入最后一个数字:”);
int y=新扫描仪(System.in).nextInt();
系统输出打印(“从“+x+”到“+y+”:”的5的倍数);
ArrayList结果=新建ArrayList();
对于(;x请尝试此操作,创建一个条件基结尾字符
System.out.print("Enter first number: ");
int x = new Scanner(System.in).nextInt();
System.out.print("Enter last number: ");
int y = new Scanner (System.in).nextInt();
System.out.print("The multiples of 5 from "+x+ " to " +y+ " : ");
if ((x<5 && y<5) || y/5 == x/5) {
System.out.printf("NONE");
}
else {
while (y > x) {
if (x%5==0) {
System.out.printf("%d", x);
x += 5;
if (x < y) {
if (x + 5 < y) {
System.out.printf(" , ");
} else {
System.out.printf(" and ");
}
}
} else {
x += 1;
}
}
}
System.out.print(“输入第一个数字:”);
int x=新扫描仪(System.in).nextInt();
系统输出打印(“输入最后一个数字:”);
int y=新扫描仪(System.in).nextInt();
系统输出打印(“从“+x+”到“+y+”:”的5的倍数);
如果((x考虑流api,如
IntStream.range(2, 8).mapToObj(Integer::toString).collect(Collectors.joining(", and "))
在printf
中使用的逗号不是简单的字符,它是模式%,d
的一部分
- 如果给出了
,'
('\u002c'
)标志,则通过扫描字符串的整数部分(从最低有效位到最高有效位)并按区域设置分组大小定义的间隔插入分隔符,插入区域设置特定的分组分隔符
您需要将其移出模式%d
,并添加一个条件以删除第一个匹配号码的逗号
for (int i = 0; x <= y; x++) {
if (x % 5 == 0) {
System.out.printf("%s%d", (i++ == 0 ? "" : ","), x);
}
}
我已经展示了两个使用,“
作为唯一分隔符的工作示例。对于三个分隔符(,“
和”,以及“
,和”和“
”),它变得有点棘手。很少出现开关语句派上用场的情况
final List<String> values = IntStream.rangeClosed(x, y)
.filter(i -> i % 5 == 0)
.mapToObj(Integer::toString)
.collect(Collectors.toList());
switch (values.size()) {
case 0:
System.out.println("NONE");
break;
case 1:
System.out.println(values.get(0));
break;
case 2:
System.out.println(String.join(" and ", values));
break;
default:
final String last = values.remove(values.size() - 1);
System.out.println(String.join(", ", values) + ", and " + last);
}
final List values=IntStream.rangeClosed(x,y)
.filter(i->i%5==0)
.mapToObj(整数::toString)
.collect(Collectors.toList());
开关(values.size()){
案例0:
系统输出打印项次(“无”);
打破
案例1:
System.out.println(values.get(0));
打破
案例2:
System.out.println(String.join(“and”,value));
打破
违约:
最终字符串last=values.remove(values.size()-1);
System.out.println(String.join(“,”,value)+和“+last);
}
这是我的解决方案:
Scanner scan = new Scanner(System.in);
System.out.print("Enter first number: ");
int x = scan.nextInt();
System.out.print("Enter last number: ");
int y = scan.nextInt();
String output = "The multiples of 5 from " + x + " to " + y + " : ";
for (; x <= y; x++) {
if (x % 5 == 0) {
output = output + x + ", ";
}
}
output = output.substring(0,output.length() -5) + " and "+output.substring(output.length() -4, output.length()-2);
System.out.println(output);
Scanner scan=新的扫描仪(System.in);
系统输出打印(“输入第一个数字:”);
int x=scan.nextInt();
系统输出打印(“输入最后一个数字:”);
int y=scan.nextInt();
字符串输出=“从“+x+”到“+y+”的5的倍数:”;
对于(;x让我从您当前代码的一些提示开始。您当前为两个用户输入创建了两个扫描仪
。最好只创建这一个,然后重复使用。因此更改:
System.out.print("Enter first number: ");
int x = new Scanner (System.in).nextInt();
System.out.print("Enter last number: ");
int y = new Scanner (System.in).nextInt();
System.out.print("The multiples of 5 from "+x+ " to " +y+ " : ");
致:
接下来,我建议将步骤变成一个变量,以便以后更容易更改(或者也可以请求用户作为输入):
现在谈谈你的实际问题。让我们首先分析一下你想要什么:
- 您希望大多数项目的分隔符为“
”,“
(基本大小写)
- 您希望迭代中倒数第二个整数的分隔符为“
”和“
- 最后一个整数不再需要任何分隔符,因为它是尾随项
现在,让我们将这些需求转换为代码:
for(; x<=y; x++){
if(x%step == 0){
// The last `x` that will be printed, is the x where the difference between y and x
// is smaller than the step-size:
boolean lastNumber = y-x < step;
// The `x` for which we want an " and " separator is the second to last item,
// so the difference between x and y should be smaller than twice the step-size:
boolean showAnd = y-x < 2*step;
// And then we can use these two booleans with a simple ternary-if to determine the
// format we'd want to use in our print:
System.out.printf(lastNumber ? "%d\n"
: showAnd ? "%d and "
: "%d, ",
x);
}
}
对于(;xcan您可以分享您的预期输出和您现在得到的内容吗?您可以将前n-1项放入一个字符串中,该字符串将有一个前导逗号,然后去掉最后一个逗号,然后附加“and”,如果最后一个逗号存在,则添加最后一个。OP希望输出像“2、3、4和5”,而不是“2、3、4和5”.我投了反对票。它对预期的输出2和3不起作用。这只是一个想法(直接说)使用流来获得一致性解决方案,在第二个流api维度发布前一分钟发布(Andrew,你的)任务本身看起来更像是一个学生的作业,所以我宁愿装备这个家伙,让他再试一次。它给我这个结果,输入第一个数字:1输入最后一个数字:15从1到15的5的倍数:5和10和15应该是:从1到15的5的倍数:5,10和15。结果将包括上限,你不需要l吗要包括的ast编号?我投了反对票,它对预期输入1和2无效。我编辑了我的解决方案。现在它应该按预期工作。