Java条件语句-分支语句

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

Java conditions- Branching statements

问题

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);

        int busHeight = s.nextInt();
        int bridgeCount = s.nextInt();
        int bridgeHeight = 0;
        boolean willCrash = false;
        int count = 0;

        for (int i = 0; i < bridgeCount; i++) {
            bridgeHeight = s.nextInt();

            if (bridgeHeight <= busHeight) { // Change the condition here
                willCrash = true;
                count++;
                System.out.println("Will crash on bridge " + (i + 1)); // Print the current bridge number
                break; // Exit the loop once a crash is detected
            }
        }

        if (!willCrash) {
            System.out.println("Will not crash");
        }
    }
}

Please note that I have made changes to your code to fix the logic issues. Make sure to replace your existing code with the one provided above.

英文:

I need to write a program that the bus will fit under bridges. The first line of the input contains the height of the bus and the number of bridges under which the bus passes. The second line contains the heights of these bridges.

I need should output "Will not crash" if everything will be all right; otherwise, output "Will crash on bridge i" (where i is a number of a bridge) into which the bus will crash. If the height of a bridge equals the height of the bus, the bus will crash.

until now I have written:

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);

        int busHeight = s.nextInt();
        int bridgeCount = s.nextInt();
        int bridgeHeight = 0;
        boolean willCrash = false;
        int count = 0;

        for (int i = 0; i &lt; bridgeCount; i++) {
            bridgeHeight = s.nextInt();

            if (bridgeHeight == busHeight || bridgeHeight &lt; busHeight) {
                willCrash = true;
                count++;
            }
            if (bridgeHeight == busHeight || bridgeHeight &lt; busHeight) {
                willCrash = true;
                count++;
            }
            if (!(bridgeCount - count != 0 &amp;&amp; willCrash)) {
                System.out.println(&quot;Will not crash&quot;);

            } else {
                System.out.println(&quot;Will crash on bridge &quot; + (bridgeCount - count));
            }
            break;
        }
    }
}

Test input:

211 5

871 205 123 871 1681

Correct output:

Will crash on bridge 2

Your code output:

Will not crash

I've been monitoring the program's launch and it's coming out prematurely. Please give me some tips on what to change.

答案1

得分: 1

以下是解决我的问题的代码。能够正确获取数据,并在公交车无法通过桥梁时进行显示。

import java.util.*;

public class Main {
    public static void main(String[] args) {
        // 写下你的代码
        Scanner scan = new Scanner(System.in);

        int heightOfBus = scan.nextInt();

        int heightOfBridges;

        int willCrash = 0;

        if (scan.hasNext()) {
            int numOfBridges = scan.nextInt();
        }

        for (int i = 1; scan.hasNext() ; i++) {

            heightOfBridges = scan.nextInt();

            if (heightOfBridges <= heightOfBus) {
                willCrash = 1;
                System.out.println("将在桥上发生碰撞 " + i);
                break;
            }
        }
        if (willCrash == 0) {
            System.out.println("不会发生碰撞");
        }
    }
}
英文:

Here is the code that solves my problem. Gets data correctly and displays when the bus does not cross the bridge

import java.util.*;

public class Main {
    public static void main(String[] args) {
        // write your code here
        Scanner scan = new Scanner(System.in);

        int heightOfBus = scan.nextInt();

        int heightOfBridges;

        int willCrash = 0;

        if (scan.hasNext()) {
            int numOfBridges = scan.nextInt();
        }

        for (int i = 1; scan.hasNext() ; i++) {

            heightOfBridges = scan.nextInt();

            if (heightOfBridges &lt;= heightOfBus) {
                willCrash = 1;
                System.out.println(&quot;Will crash on bridge &quot; + i);
                break;
            }
        }
        if (willCrash == 0) {
            System.out.println(&quot;Will not crash&quot;);
        }
    }
}

答案2

得分: 0

替代代码:

if (bridgeHeight <= busHeight) {

如果上述的 if 语句为真,你已经得出了巴士会撞上的结论,因此你可以打印:

System.out.println("将在桥上发生碰撞 " + (i+1)); // i+1 是因为在第一座桥上 i = 0,
// 在第二座桥上 i = 1,依此类推。
return; // 结束程序。

在循环结束时,你已经检查了每座桥,得出了巴士不会发生碰撞的结论,你可以打印:

System.out.println("不会发生碰撞");
英文:

Instead of

if (bridgeHeight == busHeight || bridgeHeight &lt; busHeight) {

you can use

if (bridgeHeight &lt;= busHeight) { // Bridge height is smaller or equal to the height of the bus

If the above if statement is true, you have concluded that the bus will crash, so you can print:

System.out.println(&quot;Will crash on bridge &quot; + i+1); // i+1 because i = 0 on the first bridge,
// i = 1 on the second bridge and so on.
return; // End the program.

At the end of the loop, you have checked every bridge and conclused that the bus will not crash and you can print

System.out.println(&quot;Will not crash&quot;);

huangapple
  • 本文由 发表于 2020年9月17日 22:21:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/63940123.html
匿名

发表评论

匿名网友

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

确定