mightymorphinParkRanger:
以下は、私が今持っているものです。私は知っている必要があり、主なものは、私は、配列内、文字列の文字数を確認してから、他のすべてのエントリに対してのみ、最大1を引っ張ることを確認することができる方法です。私が扱ってるの質問は次のとおりです。
空行が入力されるまで、ユーザーから名前と誕生年を読み込むプログラムを書きます。名前と誕生年は、カンマで区切られます。
その後プログラムは、最も長い名前と誕生年の平均を出力します。複数の名前が等しく、最長であれば、あなたはそれらのいずれかを印刷することができます。あなたは、ユーザが少なくとも1人に入ることを想定することができます。サンプル出力
セバスチャン、2017人のルーカス、ユリ2017年、2017年ハンナ、2014ガブリエル、2009
最長の名前:誕生年のセバスチャン平均:2014.8
import java.util.Scanner;
public class Test112 {
//Asks the user for input and will print out who is the oldest
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int checkVal = 0;
String checkName = null;
System.out.println("Provide a name and age in this format: name,#");
while (true) {
//The unique thing about this code, is each input is a separate array that is not put in memory
//You just use the array system to pull data of the values entered at that specific time
String userIn = scanner.nextLine();
if (userIn.equals("")) {
break;
}
//Here we get the info from the user, then split it
String[] valArray = userIn.split(",");
//This now uses the variable to check which value of arguments entered is the greatest
//It'll then assign that value to the variable we created
**if (checkVal < Integer.valueOf((int)valArray[0].length) {**
//We need the checkVal variable to be a constant comparison against new entries
checkVal = Integer.valueOf(valArray[1]);
//This pulls the name of the value that satisfies the argument above
checkName = valArray[0];
}
}
System.out.println("Name of the oldest: " + checkName);
scanner.close();
}
}
アービンド・クマールのAvinash:
どのように私は、配列内、文字列値の文字数をカウントして、最長のものを見つけるために他の人にそれを比較することができますか?
次のように実行します。
if (checkVal < valArray[0].length()) {
checkVal = valArray[0].length();
checkName = valArray[0];
}
注length()
の長さを見つけるために使用されますString
。
他のいくつかの重要なポイント:
- また、あなたはそれらの平均を計算することができるように、すべての出生の年の合計値を格納する変数を必要としています。また、実行中の平均値を計算することができます。
- その上で任意の操作を実行する前に、エントリ(名前と誕生年)のトリム。
- 閉じないでください
Scanner
ためSystem.in
。
下記の注意事項を取り入れた完全なプログラムです。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int checkVal = 0;
String checkName = "", name, birthYearStr;
int sum = 0, count = 0;
while (true) {
System.out.print("Provide a name and age in this format: name,#");
String userIn = scanner.nextLine();
if (userIn.equals("")) {
break;
}
String[] valArray = userIn.split(",");
name = valArray[0].trim();
birthYearStr = valArray[1].trim();
if (valArray.length == 2 && birthYearStr.length() == 4) { // Birth year should be of length 4
try {
sum += Integer.parseInt(birthYearStr);
if (checkVal < name.length()) {
checkVal = name.length();
checkName = name;
}
count++;
} catch (NumberFormatException e) {
System.out.println("Birth year should be an integer. Please try again.");
}
} else {
System.out.println("This is an invalid entry. Please try again.");
}
}
System.out.println("Longest name: " + checkName);
System.out.println("Average of birth years: " + (sum / count));
}
}
サンプルを実行します。
Provide a name and age in this format: name,#sebastian,2017
Provide a name and age in this format: name,#lucas,2017
Provide a name and age in this format: name,#lily,2017
Provide a name and age in this format: name,#hanna,2014
Provide a name and age in this format: name,#gabriel,2009
Provide a name and age in this format: name,#
Longest name: sebastian
Average of birth years: 2014
別のサンプルを実行します。
Provide a name and age in this format: name,#sebastian,201
This is an invalid entry. Please try again.
Provide a name and age in this format: name,#sebastian,hello
This is an invalid entry. Please try again.
Provide a name and age in this format: name,#sebastian,2017
Provide a name and age in this format: name,#lucas,2017
Provide a name and age in this format: name,#lily,2017
Provide a name and age in this format: name,#hanna,2014
Provide a name and age in this format: name,#gabriel,2009
Provide a name and age in this format: name,#
Longest name: sebastian
Average of birth years: 2014
別のサンプルを実行します。
Provide a name and age in this format: name,#hello,2018,sebastian,2017
This is an invalid entry. Please try again.
Provide a name and age in this format: name,#hello,2018
Provide a name and age in this format: name,#sebastian,2017
Provide a name and age in this format: name,#
Longest name: sebastian
Average of birth years: 2017
別のサンプルを実行します。
Provide a name and age in this format: name,#sebastian,rama
Birth year should be an integer. Please try again.
Provide a name and age in this format: name,#sebastian,12.5
Birth year should be an integer. Please try again.
Provide a name and age in this format: name,#sebastian,2017
Provide a name and age in this format: name,#abcdefghi,2018
Provide a name and age in this format: name,#rama,2009
Provide a name and age in this format: name,#
Longest name: sebastian
Average of birth years: 2014
別のサンプルを実行します。
Provide a name and age in this format: name,#sebastian,2017
Provide a name and age in this format: name,#lucas,2017
Provide a name and age in this format: name,#lily , 2017
Provide a name and age in this format: name,#
Longest name: sebastian
Average of birth years: 2017