2017-10-15 4 views
-3

単語と文章の同じ文字番号を同じインデックスで計算する宿題用のプログラムを作成しようとしています。2つの文字列メソッドを使ってプログラムを書く方法は?

ポイントはユーザーが文章と単語を書くことで、プログラムは同じインデックスから同じ文字を計算し、その数を表示します。しかし、単語が文よりも短い場合は、単語1の空白を右にスライドさせ、単語の長さが文の長さを超えるまで繰り返す必要があります。

そして、lengthメソッドとcharAtメソッドだけを文字列メソッドとして使用することができます。私は書くための最善の方法を見つけることができない、私は立ち往生しています。

import java.util.Scanner; 

public class Question1 { 

    public static void main(String[] args) { 

      Scanner keyboard = new Scanner(System.in); 

      // asking a sentence from user 
      System.out.println("Lütfen bir cümle giriniz."); 
      String sentence = keyboard.nextLine(); 

      //asking a word from user 
      System.out.println("Lütfen bir kelime giriniz."); 
      String word = keyboard.next(); 

      int sentenceLength = sentence.length(); 
      int wordLength = word.length(); 

      for (wordLength = 1; wordLength <= sentenceLength; wordLength++); 

      word.length(); 

答えて

0

whileを使用して行います。

import java.util.Scanner; 

public class Main { 
    public static final void main(String args[]) 
    { 
     String word, sent; 
     Scanner sc = new Scanner(System.in); 
     System.out.print("sent->"); 
     sent=sc.nextLine(); 
     System.out.print("word->"); 
     word=sc.nextLine(); 
     while(word.length()<sent.length()) 
      word = word + ' '; 
    } 
} 
関連する問題