- ベストアンサー
データの受け渡しについて
- データの受け渡しについて理解が不足しております。主に、mainの中で読み込んだデータを外部の画面出力に送る方法がわかりません。
- 現在、自分で組んだソースコードに問題があり、データの受け渡しを行うことができません。具体的なエラーメッセージも表示されず、解決策が見つかりません。
- データの受け渡しに関する基本的な概念や記述方法について不明点があります。どのように記述すればデータを正しく受け渡すことができるのか、または参考になるサイトを教えていただけないでしょうか。
- みんなの回答 (5)
- 専門家の回答
関連するQ&A
- プログラムの処理について
主クラスから別のクラスに送るときに 値一個ずつ6回送って処理した値を返すのと 値二個ずつ3回送って処理した値を返すのと どちらが早いのですか? 例1) main(){ int i,ans = 0; int num1[] = new int[6]; for(i=0;i<6;i++){ num1[i] = i+1; ans += ***.keisan(num[i]); } } class ***{ static int keisan(int num){ int x; x = num * 2; return x; } } 例2) main(){ int i,j,ans = 0; ***[] dt = new ***[3]; for(i=0,j=1;i<3;i++){ *** dt[i] = new ***(); dt[i].num1 = dt[i].num2 = 0; dt[i].num1 = j++; dt[i].num2 = j++; ans += ***.keisan(dt[i].num1,dt[i].num2); } } class ***{ public int num1; public int num2; static int keisan(int num1,int num2){ int x; x = (num1 + num2) * 2; return x; } } 上記の構文みたいな感じの状況です 構文が間違ってても気にしないでいただけるとありがたいです
- ベストアンサー
- Java
- クラスのフィールドの値を配列に代入する方法
フィールドの値を代入したいのですが、 java.lang.ArrayIndexOutOfBoundsException というエラーが出てしまい、どうすればいいのかわかりません。 どのように対処すればいいのでしょうか? class Car { String carname; int height; int width; int length; } class public static void main(String[] args) throw IOException { Car car[]; car = new Car[5]; System.out.println("車の情報を入力してください。"); BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in); for(int i=0; i<5; i++){ car[i] = new Car(); System.out.println(i+1 + "台目の車の名前を入力してください。"); String str1 = br1.readLine(); System.out.println("車の高さを入力してください。"); String str2 = br1.readLine(); System.out.println("車の幅を入力してください。"); String str3 = br1.readLine(); System.out.println("車の長さを入力してください。"); String str4 = br1.readLine(); int h1 = Integer.parseInt(str2); int w1 = Integer.parseInt(str3); int l1 = Integer.parseInt(str4); car[i].carname = str1; car[i].height = h1; car[i].weith =w1; car[i].length =l1; } for(int i=0; i<5; i++){ int c_h[] = new int[5]; c_h[i] = car[i].height; } } よろしくお願いします。
- ベストアンサー
- Java
- クラスのフィールドの比較方法
2つのクラスのフィールドを比較して、その結果を以下のように表示させたいのですが、どのようにプログラミングをすればいいのでしょうか? 例 class Car { String carname; int height; int width; int length; } class Garage { String garagename; int height; int width; int depth; } public class example { public static void main (String[] args) throws IOException { Car car[]; car = new Car[5]; System.out.println("車の情報を入力してください。"); BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in); for(int i=0; i<5; i++){ car[i] = new Car(); System.out.println(i+1 + "台目の車の名前を入力してください。"); String str1 = br1.readLine(); System.out.println("車の高さを入力してください。"); String str2 = br1.readLine(); System.out.println("車の幅を入力してください。"); String str3 = br1.readLine(); System.out.println("車の長さを入力してください。"); String str4 = br1.readLine(); int h1 = Integer.parseInt(str2); int w1 = Integer.parseInt(str3); int l1 = Integer.parseInt(str4); car[i].carname = str1; car[i].height = h1; car[i].weith =w1; car[i].length =l1; } for(int i=0; i<5; i++){ garage[i] = new Garage(); System.out.println(i+1 + "つ目の車庫の名前を入力してください。"); String str5 = br2.readLine(); System.out.println("車庫の高さを入力してください。"); String str6 = br2.readLine(); System.out.println("車庫の幅を入力してください。"); String str7 = br2.readLine(); System.out.println("車庫の長さを入力してください。"); String str8 = br2.readLine(); int h2 = Integer.parseInt(str6); int w2 = Integer.parseInt(str7); int d2 = Integer.parseInt(str8); garage[i].garagename = str2; garage[i].height = h2; garage[i].weith =w2; garage[i].depth =d2; } } 出力例 garage1 car3 garage2 car5 garage3 car4 garage4 car2 garage5 car1 よろしくお願いします。
- ベストアンサー
- Java
- 配列で最大・最小値を求める
import java.io.*; class Prog10_2 { public static void main(String args[])throws IOException { System.out.println("10個の整数を入力してください。"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int num[] = new int[10]; for(int i=0; i<num.length; i++){ String str = br.readLine(); int tmp = Integer.parseInt(str); num[i] = tmp; } int max = 0; int min = 0; for(int i=0; i<num.length; i++){ if(max < num[i]){ max = num[i]; } if(min > num[i]){ min = num[i]; } } System.out.println("最大値は"+max+"で、最小値は"+min+"です。"); } } というコマンドを実行すると、最大値はちゃんと表示されるのですが、 最小値はいつも"0"しか表示されません。どこを修正すればいいのでしょうか? どなたか分かる方いらっしゃいましたら、よろしくお願いします。
- ベストアンサー
- Java
- 動的配列の受け渡しについて
動的に定義した2次元配列を受け渡す方法がわからなくて困ってます。 配列に-1以外が入っている場合の行数を数えるプログラムを作成しています。 しかし、コンパイルすると下のエラーが出てコンパイルできません。 error:cannot convert 'long int **' to 'long int (*)[2] for argument '1' to 'long int count_line(long int (*)[2])' #include <stdio.h> long count_line(long arrau[][2]); int main(void){ long** test = new long* [100]; for(long num1=0 ; num1<100 ; num1++){ test[num1] = new long [2]; } for(int i=0 ; i<100 ; i++){ test[i][0]=-1; test[i][1]=-1; } for(int num2=0 ; num2<10 ; num2++){ test[num2][0]=1; test[num2][1]=1; } int x=0; x = count_line(test); printf("x=%d\n",x); } long count_line(long array[][2]){ long count=0; long end=0; long roop=0; while(end!=-1){ if(array[roop][1]!=-1){ count++; } end=array[roop][1]; roop++; } return(count); } どなたかお助けをー(T_T)
- ベストアンサー
- C・C++・C#
- 初心者Javaの件。
いつも大変お世話になりありがとうございます。 Javaの基本的なプログラムについてです。 コードとエラーメッセージを記述致しました。 どう治せばいいか、教えてください。 ご回答のほど、宜しくお願い申し上げます。 import java.io.BufferedReader; public class Sample3 { { public static void main(String args[]) throws Exception { person p[]; System.out.println("人数を入力してください。"); BufferedReader br = new BufferedReader(new inputStreamReader(System.in)); String str = br.readLine(); int num = Integer.parseInt(str); p = new Person[num]; for(int i=0; i<num; i++) { } System.out.println("年齢を入力してください"); str = br.readLine(); int age = Integer.parseInt(str); P = new Person[num]; for(int i=0; i<num; i++){ System.out.println("年齢を入力してください"); str = br.readLine(); int age = Intger.parseInt(str); System.out.println("体重を入力してください"); str = br.readLine(); double weight = Double.parseDouble(str); System.out.println("身長を入力してください"); str = br.readLine(); double height = Double.parseDouble(str); p[i] = new person(age, weight, height); } for(int i=0; i<num; i++) { p[i].show(); } } Exception in thread "main" java.lang.Error: Unresolved compilation problems: person を型に解決できません inputStreamReader を型に解決できません Person を型に解決できません P を変数に解決できません Person を型に解決できません トークンに構文エラーがあります。これらのトークンを削除してください 重複ローカル変数 age Intger を解決できません トークンに構文エラーがあります。これらのトークンを削除してください person を型に解決できません at Sample3.main(Sample3.java:7)
- ベストアンサー
- Java
- ポインタ配列の問題で、、。
ポインタ配列の問題ですが、このmain関数でどこかがおかしいのですが、どこをどう変更すれば正常に動作するのかわかりません。どなたかお願いします(注)string入力文字は9文字以下です。 #include <stdio.h> #define NUM 5 main(void) { char *str[NUM]; int i; for(i = 0; i < NUM; i++) { printf("string ->"); scanf("%s", str[i]); } for(i = 0; i < NUM; i++) { printf("str[%d] -> %s\n", i, str[i]); } }
- ベストアンサー
- C・C++・C#
- コンストラクタでvar ?
javascriptのコンストラクタについて。 下記のコードで、変数を2つ宣言しています(★)が、 下部のfor文で、その変数名を引数のstr,numにすれば、★での宣言は不要ですか? 宣言しとくと何かメリットがあるのでしょうか? var Repeater = function(str, num){ var string = str, //★ number = num; //★ this.repeat = function (){ var i, result = ""; for(i = 0; i < number; i++ ) { result += string; } return result; }; }; new Repeater("abc", 3).repeat(); あるサイトから引用
- ベストアンサー
- JavaScript
- ファイルからデータをうまく読み込めません
ファイルからデータを読み込んでクラスに宣言した変数に格納したいのですがうまく出来ません ------------------------------------------ import java.io.*; class date{ public String name; public int ko,su; } public class Helloworld { public static void main(String args[]){ date[] dt = new date[2]; BufferedReader br = new BufferedReader(new FileReader("フルパス")); String line; try{ int i=0; while((line = br.readLine())!=null){ dt[i].ko = dt[i].su = 0; dt[i].name = line; line = br.readLine(); dt[i].ko = Integer.valueOf(line).intValue(); line = br.readLine(); dt[i].su = Integer.valueOf(line).intValue(); System.out.println("名前:" + dt[i].name); System.out.println("国語:" + dt[i].ko); System.out.println("数学:" + dt[i].su); i++; } br.close(); }catch(Exception e){ System.out.println("エラー情報:" + e); } } } -------------------------- 上記のソースで実行すると java.lang.NullPointerException とエラーが出てます FileReadernの部分にカーソルを当てると この要素には接続されたソースも、接続された Javadoc もないため、情報を検出できませんでした と表示されました
- ベストアンサー
- Java
お礼
大変参考になりました プログラムの構文とか下手ですいませんm(_ _)m 画面に出力とかファイル読み込み単体なら 出来たんですけどどちらも使うとなると うまくかけませんでした 貴重な回答ありがとうございました