移除for循环中的最后一个逗号(Java)

huangapple go评论132阅读模式
英文:

removing the last comma in for loops (java)

问题

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner dev = new Scanner(System.in);
        int num1, num2;
        num1 = dev.nextInt();
        num2 = dev.nextInt();

        int numa = 1;
        System.out.println();
        for (numa = num1; numa <= 100; numa++) {
            if (numa / num1 == 1 && numa % num1 == 0)
                System.out.print(numa);
            else if (numa % num1 == 0)
                System.out.print("," + numa);

        }
        int numb = 1;
        System.out.println();
        for (numb = num2; numb <= 100; numb++) {
            if (numb / num2 == 1 && numb % num2 == 0)
                System.out.print(numb);
            else if (numb % num2 == 0)
                System.out.print("," + numb);

        }

        int numc = 1;
        System.out.println();
        for (numc = num1; numc <= 100; numc++) {
            if (numc / num2 == 2 && numb % num2 == 0)
                System.out.print(numc);
            else if (numc % num1 == 0 && numc % num2 == 0)
                System.out.print(numc + ",");
        }
    }
}
英文:

I'm new to coding and I'm trying to remove the last comma for my last loop.
It would be great if I can get help!

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner dev = new Scanner(System.in);
int num1, num2;
num1 = dev.nextInt();
num2 = dev.nextInt();
int numa = 1;
System.out.println();
for (numa = num1; numa &lt;= 100; numa++) {
if (numa / num1 == 1 &amp;&amp; numa % num1 == 0)
System.out.print(numa);
else if (numa % num1 == 0)
System.out.print(&quot;,&quot; + numa);
}
int numb = 1;
System.out.println();
for (numb = num2; numb &lt;= 100; numb++) {
if (numb / num2 == 1 &amp;&amp; numb % num2 == 0)
System.out.print(numb);
else if (numb % num2 == 0)
System.out.print(&quot;,&quot; + numb);
}
int numc = 1;
System.out.println();
for (numc = num1; numc &lt;= 100; numc++) {
if(numc / num2 == 2 &amp;&amp; numb % num2 == 0)
System.out.print(numc);
else if(numc % num1 == 0 &amp;&amp; numc % num2 == 0)
System.out.print(numc + &quot;,&quot;);
}
}
}

答案1

得分: 0

根据@Lino的评论,将您的最后一个循环更改为:

for (numc = num1; numc <= 100; numc++) {
    if(numc / num2 == 2 && numb % num2 == 0)
        System.out.print(numc);
    else if(numc % num1 == 0 && numc % num2 == 0){
        if(numc < 100) 
            System.out.print(numc + ",");
        else
            System.out.print(numc);
    }
}

一个更好的方法是将数字添加到一个列表中(例如 numbersToPrint),然后使用 Collectors.joining()

String output = numbersToPrint.stream().map(i-> String.valueOf(i)).collect(Collectors.joining(","));
// output: 1,2,...,100

或者使用 StringJoiner

StringJoiner sj = new StringJoiner(",");
sj.add(String.valueOf(1)).add(String.valueOf(2));
String output = sj.toString();
// output: 1,2

或者使用 String.join

String output = String.join(",", String.valueOf(1),String.valueOf(2));
// output: 1,2
英文:

Following the comment of @Lino, change your last loop to:

for (numc = num1; numc &lt;= 100; numc++) {
if(numc / num2 == 2 &amp;&amp; numb % num2 == 0)
System.out.print(numc);
else if(numc % num1 == 0 &amp;&amp; numc % num2 == 0){
if(numc &lt; 100) 
System.out.print(numc + &quot;,&quot;);
else
System.out.print(numc);
}
}

A better way would be to add the numbers to a list (for example numbersToPrint) and then using Collectors.joining():

String output = numbersToPrint.stream().map(i-&gt; String.valueOf(i)).collect(Collectors.joining(&quot;,&quot;));
// output: 1,2,...,100

Or using StringJoiner:

StringJoiner sj = new StringJoiner(&quot;,&quot;);
sj.add(String.valueOf(1)).add(String.valueOf(2));
String output = sj.toString();
// output: 1,2

Or using String.join:

String output = String.join(&quot;,&quot;, String.valueOf(1),String.valueOf(2));
// output: 1,2

答案2

得分: 0

我建议以下内容作为更标准的写法,因此对于长时间使用 Java 的程序员来说更易读(使用 i 作为整数循环计数器,倾向于使用 &lt; 而不是 &lt;= 进行计数,简化数学表达式):

System.out.println();
boolean lastWasEqual = false;
for (int i = num1; i < 101; i++) {
    boolean isHalf = (2 * i == num2);  // a/b = 2 <=> 2a = b
    boolean isEqual = (i == num2);     // (a%b) = (b%a) = 0 <=> a = b

    if (isHalf || isEqual) {
       if (lastWasEqual) {
          System.out.print(",");
       }
       System.out.print(i);
       lastWasEqual = isEqual;
    }
}

这里的思想是,我们只在以下情况下输出逗号 ,:(a) 它已准备好打印(lastWasEqual),以及 (b) 在它之后将会有输出(以便 "最后一个逗号" 永不被打印出来)。

通过查看数学表达式,您可以看出 isEqual 最多只会为 true 一次,而 isHalf 也最多只会为 true 一次。并且很容易看出它们何时会为 true。因此不需要循环:

System.out.println();
boolean willReachEqual = (num1 <= num2) && (num2 <= 100);
boolean willReachHalf  = (num1 * 2 <= num2) 
                       && ((num2 % 2) == 0) && (num2 <= 200);
boolean willReachBoth = willReachEqual && willReachHalf;

System.out.println(
           (willReachHalf ? (num2 / 2) : "") + 
           (willReachBoth ? "," : "") + 
           (willReachEqual ? num2 : ""));
英文:

I would recommend the following as much more standard, and therefore, for long-time java programmers, readable (uses i for integer loop counter, prefers counting to &lt; instead of &lt;=, simplifies math expressions):

    System.out.println();
boolean lastWasEqual = false;
for (int i = num1; i &lt; 101; i++) {
boolean isHalf = (2 * i == num2);  // a/b = 2 &lt;=&gt; 2a = b
boolean isEqual = (i == num2);     // (a%b) = (b%a) = 0 &lt;=&gt; a = b
if (isHalf || isEqual) {
if (lastWasEqual) {
System.out.print(&quot;,&quot;); 
}
System.out.print(i);
lastWasEqual = isEqual;
}
}

The idea here is that we only output a , once we know that (a) it was ready to be printed (lastWasEqual) and (b) there will be output after it (so that "the last comma" is never printed).

By looking at the math, you can see that isEqual will be true at most once, and isHalf will also be true at most once. And it is easy to see when they will be true. So there is no need for loops:

 System.out.println();
boolean willReachEqual = (num1 &lt;= num2) &amp;&amp; (num2 &lt;= 100);
boolean willReachHalf  = (num1 * 2 &lt;= num2) 
&amp;&amp; ((num2 % 2) == 0) &amp;&amp; (num2 &lt;= 200);
boolean willReachBoth = willReachEqual &amp;&amp; willReachHalf;
System.out.println(
(willReachHalf ? (num2 / 2) : &quot;&quot;) + 
(willReachBoth ? &quot;,&quot; : &quot;&quot;) + 
(willReachEqual ? num2 : &quot;&quot;));

huangapple
  • 本文由 发表于 2020年9月28日 16:58:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64098991.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定