Java 如何仅打印阵列列表中的最大编号?
我的项目文本文件中有一系列单词。我试图区分文件中的大写字母,只打印出它能找到的最大数字。例如输入: 滚潮滚滚 我的输出:2R 我想有代码可以找到最大计数之类的,但我现在迷路了 以下是我迄今为止的代码:Java 如何仅打印阵列列表中的最大编号?,java,Java,我的项目文本文件中有一系列单词。我试图区分文件中的大写字母,只打印出它能找到的最大数字。例如输入: 滚潮滚滚 我的输出:2R 我想有代码可以找到最大计数之类的,但我现在迷路了 以下是我迄今为止的代码: import java.io.*; import java.util.Scanner; import java.io.IOException; import java.io.FileInputStream; import java.io.FileNotFoundException; public
import java.io.*;
import java.util.Scanner;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
public class letters {
public static void main(String[] args) throws FileNotFoundException {
FileInputStream fis = new FileInputStream("input.txt");
Scanner scanner = new Scanner(fis);
String str = scanner.nextLine();
System.out.println(str);
int upperCaseCounter = 0;
int upperCase[] = new int[26];
while (scanner.hasNextLine()) {
String s = scanner.nextLine();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (Character.isAlphabetic(ch)) {
if (Character.isUpperCase(ch)) {
upperCase[ch - 'A']++;
System.out.println(ch + " : " + upperCase[ch - 'A']);
}
}
}
}
}
}
我只需要打印R:12
你是怎么做到的。任何帮助都将不胜感激。谢谢
我对这个网站上的凹痕是新的,我试着快点 您可以使用方法查找数组中的最大或最小数
Arrays.sort(upperCase);
int maxIndex = upperCase.length-1;
System.out.println("Max element is:"+(char)upperCase[maxIndex])+":"+upperCase[maxIndex]);
sort()
方法按升序对数组进行排序。然后数组的第一个元素是min
number,最后一个元素是max
注意:上面的代码应该在while循环之后,以便它只打印一次,而不是像您的情况那样打印多次。您可以使用方法查找数组中的最大或最小数字
Arrays.sort(upperCase);
int maxIndex = upperCase.length-1;
System.out.println("Max element is:"+(char)upperCase[maxIndex])+":"+upperCase[maxIndex]);
sort()
方法按升序对数组进行排序。然后数组的第一个元素是min
number,最后一个元素是max
注意:上面的代码应该在while循环之后,以便它只打印一次,而不是像您的情况那样打印多次。在字符递增后,您可以在while循环中使用一个变量,例如MaxVal。然后使用if语句将新分配的递增值(大写[ch-'A'])与变量MaxVal进行比较。如果大于MaxVal,则为MaxVal指定大写[ch-'A']的值 您可能会将MaxVal设置为二维数组,以保存字符及其当前计数
祝你好运 字符递增后,可以在while循环中使用变量,例如MaxVal。然后使用if语句将新分配的递增值(大写[ch-'A'])与变量MaxVal进行比较。如果大于MaxVal,则为MaxVal指定大写[ch-'A']的值 您可能会将MaxVal设置为二维数组,以保存字符及其当前计数
祝你好运 或者,您可以计算for循环中的最大值。请运行我的代码
public static void main(String[] args) throws FileNotFoundException {
FileInputStream fis = new FileInputStream("input.txt");
Scanner scanner = new Scanner(fis);
String str = scanner.nextLine();
System.out.println(str);
int upperCaseCounter = 0;
int upperCase[] = new int[26];
int max=0;
char let='A';
while (scanner.hasNextLine()) {
String s = scanner.nextLine();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (Character.isAlphabetic(ch)) {
if (Character.isUpperCase(ch)) {
upperCase[ch - 'A']++;
// System.out.println(ch + " : " + upperCase[ch - 'A']);
if(max<upperCase[ch - 'A']){
max=upperCase[ch - 'A'];
let=ch;
}
}
}
}
}
System.out.println(let+" "+max);
}
}
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException{
FileInputStream fis=新的FileInputStream(“input.txt”);
扫描仪=新扫描仪(fis);
String str=scanner.nextLine();
系统输出打印项次(str);
int大写计数器=0;
int大写[]=新int[26];
int max=0;
charlet='A';
while(scanner.hasNextLine()){
字符串s=scanner.nextLine();
对于(int i=0;i 如果(max或者,您可以在for循环中计算最大值。请运行我的代码
public static void main(String[] args) throws FileNotFoundException {
FileInputStream fis = new FileInputStream("input.txt");
Scanner scanner = new Scanner(fis);
String str = scanner.nextLine();
System.out.println(str);
int upperCaseCounter = 0;
int upperCase[] = new int[26];
int max=0;
char let='A';
while (scanner.hasNextLine()) {
String s = scanner.nextLine();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (Character.isAlphabetic(ch)) {
if (Character.isUpperCase(ch)) {
upperCase[ch - 'A']++;
// System.out.println(ch + " : " + upperCase[ch - 'A']);
if(max<upperCase[ch - 'A']){
max=upperCase[ch - 'A'];
let=ch;
}
}
}
}
}
System.out.println(let+" "+max);
}
}
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException{
FileInputStream fis=新的FileInputStream(“input.txt”);
扫描仪=新扫描仪(fis);
String str=scanner.nextLine();
系统输出打印项次(str);
int大写计数器=0;
int大写[]=新int[26];
int max=0;
charlet='A';
while(scanner.hasNextLine()){
字符串s=scanner.nextLine();
对于(int i=0;i 如果(max您需要保留两个局部变量int-temp和char-ch1来跟踪您的最大长度和相应的校正器。
包com.mindtree.programs
导入java.util.array;
导入java.util.Scanner
导入java.io.FileInputStream;
导入java.io.FileNotFoundException
公共类ReadScacnner{
public static void main(String[] args) throws FileNotFoundException {
FileInputStream fis = new FileInputStream("input.txt");
Scanner scanner = new Scanner(fis);
int upperCase[] = new int[26];
int temp = 0;
char ch1 = 0;
while (scanner.hasNextLine()) {
String s = scanner.nextLine();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (Character.isLetter(ch)) {
if (Character.isUpperCase(ch)) {
upperCase[ch - 'A']++;
if (temp < upperCase[ch - 'A']) {
ch1 = ch;
temp = upperCase[ch - 'A'];
}
}
}
}
}
Arrays.sort(upperCase);
System.out.println("Max element is:" + ch1 + " : "
+ upperCase[upperCase.length - 1]);
}
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException{
FileInputStream fis=新的FileInputStream(“input.txt”);
扫描仪=新扫描仪(fis);
int大写[]=新int[26];
内部温度=0;
charch1=0;
while(scanner.hasNextLine()){
字符串s=scanner.nextLine();
对于(int i=0;i
}您需要保留两个局部变量int-temp和char-ch1来跟踪最大长度和相应的校正器。这里我给出修改后的代码。
包com.mindtree.programs
导入java.util.array;
导入java.util.Scanner
导入java.io.FileInputStream;
导入java.io.FileNotFoundException
公共类ReadScacnner{
public static void main(String[] args) throws FileNotFoundException {
FileInputStream fis = new FileInputStream("input.txt");
Scanner scanner = new Scanner(fis);
int upperCase[] = new int[26];
int temp = 0;
char ch1 = 0;
while (scanner.hasNextLine()) {
String s = scanner.nextLine();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if (Character.isLetter(ch)) {
if (Character.isUpperCase(ch)) {
upperCase[ch - 'A']++;
if (temp < upperCase[ch - 'A']) {
ch1 = ch;
temp = upperCase[ch - 'A'];
}
}
}
}
}
Arrays.sort(upperCase);
System.out.println("Max element is:" + ch1 + " : "
+ upperCase[upperCase.length - 1]);
}
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException{
FileInputStream fis=新的FileInputStream(“input.txt”);
扫描仪=新扫描仪(fis);
int大写[]=新int[26];
内部温度=0;
charch1=0;
while(scanner.hasNextLine()){
字符串s=scanner.nextLine();
对于(int i=0;i