2017-02-22 10 views
-5
import java.util.Scanner; 
public class Bigger{ 

public static void main(String [] args) 
{ 
// declare variables 
Scanner keyboardIn = new Scanner(System.in); 
String userName = new String(); 
String fName = new String(); 
int numberLetters = 0; 
int bigLetters=0; 
char firstLetter; 

// get user name from the user 
System.out.print("Please enter your user name: "); 
userName = keyboardIn.nextLine(); 

// get second name from the user 
System.out.print("Please enter your second name: "); 

fName = keyboardIn.nextLine(); 

// use an appropriate method to find the number of letters 
numberLetters = userName.length(); 
bigLetters = fName.length(); 

if(numberLetters > bigLetters) 
{ 
    System.out.print("String 1 Is the longest string "); 
} 
else 
{ 
    System.out.print("String 2 Is the longest string "); 
} 
} 
} 

を入力したものを返すために、これを必要とします。私はこれをどのようにして行うのですか?私はデンマークが、私はそれがユーザーにこれをプリントアウトする必要が大きな文字列であるかのように、実際の文字と実際の文字列をプリントアウトするために、これを必要

よろしく、

マーク

+0

スタート、それを試してみてください。 – bejado

+2

これはコードレビューの場所ではありません。コードに関する特定の質問がある場合は、明示的に質問する必要があります。さもなければ人々はあなたのためにあなたのコードをデバッグすることはまずありません。 – Encaitar

+0

So ... 'System.out.print(userName +"最長の文字列です ");'? –

答えて

0

あなたが実際にも、入力された文字列が(必要に応じて、以下のが<=>=もしくは以下を使用して使用して以上にチェックするために短縮することが可能)長さが等しいかどうかを確認する必要があります。

import java.util.Scanner; 

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

    // get user name from the user 
    System.out.print("Please enter your user name: "); 
    String userName = input.nextLine(); 
    // get second name from the user 
    System.out.print("Please enter your second name: "); 
    String secondName = input.nextLine(); 

    // use an appropriate method to find the number of letters and prompt user 
    if(userName.length() == secondName.length()) { 
     System.out.println(userName + " is equal in length than " + secondName); 
    } else if(userName.length() > secondName.length()) { 
     System.out.println(userName + " is longer in length than " + secondName); 
    } else { 
     System.out.println(userName + " is shorter in length than " + secondName); 
    } 
    } 
} 

使用例:

Please enter your user name: MarkDoherty 
Please enter your second name: Denmark 
MarkDoherty is longer in length than Denmark 

代わりにあなたがそうのような書式設定文字列を使用することができます。

// use an appropriate method to find the number of letters 
if(userName.length() == secondName.length()) { 
    System.out.printf("%s (%d characters long) is equal in length than %s (%d characters long)\n", userName, userName.length(), secondName, secondName.length()); 
} else if(userName.length() > secondName.length()) { 
    System.out.printf("%s (%d characters long) is longer in length than %s (%d characters long)\n", userName, userName.length(), secondName, secondName.length()); 
} else { 
    System.out.printf("%s (%d characters long) is shorter in length than %s (%d characters long)\n", userName, userName.length(), secondName, secondName.length()); 
} 

使用例:

Please enter your user name: MarkDoherty 
Please enter your second name: Denmark 
MarkDoherty (11 characters long) is longer in length than Denmark (7 characters long) 

あなたが経験している何の問題私たちに伝えることによりhere!

0

私が正しくあなたを理解していた場合は、最も簡単な方法は、ちょうどあなたがSystem.out.print()の内部変数をプリントアウトする必要がどんな文字列連結するだろう。あなたが1行上のすべては、あなたの文字列の末尾に改行を追加したりしていることを確認したい場合を除き、また

System.out.print("This is my string variable" + myString); 

:たとえば、私はmyStringと呼ばれるタイプStringの変数を持っている場合、私は、コードの行を使用しますSystem.out.println()を使用してください。

+0

ご協力ありがとうございます。皆さんはとても役に立ちました。 –

+0

私の質問に答えました。私はちょうどユーザーから2つの名前を受け入れ、最も長い名前を印刷するプログラムが必要でした。私はそれが変数のどれが最も長い名前を持っていたかを表示することしかできなかったので、変数1は大きかったし、変数2は大きかったと言っていました。私はそれが最大の変数とその変数の内容を返すようにしたいと思いました。私はあなたがすべて非常に敏感であることを知っているので、次回に質問することにもっと気を配ります。 –

関連する問題