Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Update Exercise_05_41.java#62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
JustLeafy2003 wants to merge1 commit intojsquared21:master
base:master
Choose a base branch
Loading
fromJustLeafy2003:patch-1
Open
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 36 additions & 37 deletionsExercise_05/Exercise_05_41/Exercise_05_41.java
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,39 @@
/*
(Occurrence of max numbers) Write a programthatreads integers, finds the largest
of them, and counts its occurrences. Assume thattheinput ends with number
0. Suppose that you entered 3 5 2 5 5 5 0;theprogram finds that the largest
is 5 and the occurrence count for 5 is 4.
/* 4.Write a program that reads integers, finds the largest of them, and
counts its occurrences. Assumethatthe input ends with number 0.
Suppose that you entered 3 5 2 5 5 5 0;theprogram finds that the largest is
5 andtheoccurrence count for 5 is 4.

(Hint: Maintain two variables, max and count. max stores the current max number,
and count stores its occurrences. Initially, assign the first number to max
and1 to count. Compare each subsequent number with max. If the number is
greaterthan max, assign it to max and reset count to 1. If the number is equal to
max,increment count by 1.)
and count stores its occurrences. Initially, assign the first number to max and
1 to count. Compare each subsequent number with max. If the number is greater
than max, assign it to max and reset count to 1. If the number is equal to max,
increment count by 1.)
*/
import java.util.Scanner;

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

// Prompt the user to enter number
System.out.print("Enter numbers: ");
int max = input.nextInt();// Assign the first number to max
int count = 1;// Assign 1 to count
int number;// Holds future inputs

// Assume that the input ends with number 0
while (number > 0) {
number = input.nextInt();
if (number > max) {
max = number;
count = 1;
}
if (number == max)
count++;
}

// Display to results
System.out.println("The largest number is " + max);
System.out.println(
"The occurrence count of the largest number is " + count);
}
}
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); //Create Scanner

//Prompt the user to enter a series of numbers
System.out.print("Enter a series of numbers: ");
int max = input.nextInt(); //Assign the numbers as max
int count = 0; //Initialize count as 0
int number; //Placeholder for the other numbers

do {
number = input.nextInt();
//Find max
if (number > max) {
max = number;
}
//Count occurrence for max number
if (number == max)
count++;
} while (number > 0);

//Display the results
System.out.println("The largest number is " + max);
System.out.println("The occurrence count of the largest number is "
+ count);
}
}

[8]ページ先頭

©2009-2025 Movatter.jp