2025-Find the largest element (java)

Insert picture description here
Idea : Refer to the StringBuilder object, because there is a convenient way to change and add the word of this object. String cannot be inserted. The
specific process is to find the largest character, and then use for to traverse and compare the largest character, add the change into the StringBuilder, and the output is can.

import java.util.*;
public class Main {
    
    
public static void main(String[] args) {
    
    
	Scanner a=new Scanner(System.in);
	while(a.hasNext())
	{
    
    
		String str=a.next();						
		StringBuilder string=new StringBuilder();	//创建StringBuilder对象
		char ch=str.charAt(0);
		for(int i=0;i<str.length();i++)				//进行最大数组遍历并标记
		{
    
    
			if(str.charAt(i)>ch)
				ch=str.charAt(i);
		}
		for(int j=0;j<str.length();j++)				//找出最大字符,并更改输入到StringBuilder中
		{
    
    
			if(str.charAt(j)==ch)
				string.append(str.charAt(j)+"(max)");
			else
				string.append(str.charAt(j));
		}
		System.out.println(string);  				//输出string对象
	}
}
}

If there is an error, please correct me

Guess you like

Origin blog.csdn.net/weixin_45956604/article/details/113824723