• ベストアンサー

プログラミングの質問です

gfe01045の回答

  • gfe01045
  • ベストアンサー率61% (19/31)
回答No.2

askaaska さんの言うとうりです!! cx xy の変数宣言がしっかりできていれば、これでよいにでは・・・ とにかくコンパイルは通るのですか? >> cx[i], cy[i], lengthすべてに とありますが、これは変数部分に関してのことのようです しかしこの構文だけでは判断できかねますが?

関連するQ&A

  • プログラミングの質問です

    javaをやってます。 開発環境はeclipseです。 コンパイルエラーで分からないことがあります。 for(int i=0 [i]<cx.length; i++) cx[i] -= 2; if(cx[i]<-409) cx[i] = 480; g.drawImage(img_coloud, cx[i], cy[i], mainwindow); 例えばこの文です。本を見ながら同じように記述しているのですが、 i=0 [i]<cxとか i++)とかに赤破線が引かれてしまいます。(例えばです) その文を消すと全く違うところに破線が出るみたいになります。 エラーは基本的には文法が間違ってるというのは一応知ってます。 どう間違ってるのかよく分かりません。 こういうときはどう対処したらいいですか?

    • ベストアンサー
    • Java
  • Java言語プログラミングに関して質問です

    以下の問いでソースを作りました。 問.2次元上の円と長方形を表すデータファイルが与えられたとき、以下の処理を行うプログラムを作成せよ。ただし、長方形は必ず座標軸に並行変を持つとする。 1.全図形の合計面積を表示せよ 2.一番面積の大きい図形データを表示せよ 3.周の長さ順に図形データを表示せよ 4.重なり合ってるすべての円のペアを列挙せよ 5.重なり合っているすべての図形のペアも列挙せよ 図形データの例 1. 1, 50.0, 50.0, 50.0 2. 2, -100.0, 100.0, 0.0, 0.0 3. 1, 0.0, 0.0, 10.0 4. 1, 50.0, 0.0, 30.0 5. 2, -75.0, 75.0, -25.0, 25.0 class Shape{ int id; int type; public double getArea() { return 0.0; } public double getPerimeter() { return 0.0; } } class Circle extends Shape{ double cx,cy; double r; Circle(int id, int type, double cx, double cy, double r){ this.id = id; this.type = type; this.cx = cx; this.cy = cy; this.r = r; } public double getArea(){ return 3.14159265 * r * r; } public double getPerimeter(){ return 3.14159265 * 2 * r; } } class Rectangle extends Shape{ double lx,ly,rx,ry; Rectangle(int id,int type,double lx,double ly,double rx,double ry){ this.id = id; this.type = type; this.lx = lx; this.ly = ly; this.rx = rx; this.ry = ry; } public double getArea(){ return (rx - lx) * (ly - ry); } public double getPerimeter(){ return 2 * (ly - ry) + 2* (lx - rx); } } class ShapeTest{ public static void main(String[] args) { Shape shapes[] = new Shape[5]; shapes[0] = new Circle(1, 1, 50.0, 50.0, 50.0); shapes[1] = new Rectangle(2, 2, -100.0, 100.0, 0.0, 0.0); shapes[2] = new Circle(3, 1, 0.0, 0.0, 10.0); shapes[3] = new Circle(4, 1, 50.0, 0.0, 30.0); shapes[4] = new Rectangle(5, 2, -75.0, 75.0, -25.0, 25.0); double sum = 0.0; for(int i=0;i<shapes.length;i++){ sum += shapes[i].getArea(); } System.out.println("合計面積は " +sum); Shape maxShape = shapes[0]; for(int i=1;i<shapes.length;i++){ if(shapes[i].getArea() > maxShape.getArea()){ maxShape = shapes[i]; } } System.out.println("一番面積の大きい図形は ID=" + maxShape.id); for(int i=0;i<shapes.length-1;i++){ for(int j=i+1;j<shapes.length;j++){ if(shapes[j].getArea() > shapes[i].getArea()){ Shape tmp = shapes[i]; shapes[i] = shapes[j]; shapes[j] = tmp; } } } System.out.println("周の長さの大きい順は "); for(int i=0;i<shapes.length;i++){ System.out.print(" " +shapes[i].id); } System.out.println(); } } //実行結果 合計面積は 23495.574275 一番面積の大きい図形は ID=2 周の長さの大きい順は 2 1 4 5 3 この問題ですが、周の長さの大きい順は2→1→5→4→3になり、問4、5ができていません。どうしたらよろしいですか?教えてください。解説とソースをお願いします。

  • Java言語プログラミングについて質問です

    以下の問で以下のようなソースを作りました。 問.2次元上の円と長方形を表すデータファイルが与えられたとき、以下の処理を行うプログラムを作成せよ。ただし、長方形は必ず座標軸に並行変を持つとする。 1.全図形の合計面積を表示せよ 2.一番免責の大きい図形データを表示せよ 3.周の長さ順に図形データを表示せよ 4.重なり合ってるすべての円のペアを列挙せよ class Shape{ int id; int type; public double getArea() { return 0.0; } public double getPerimeter() { return 0.0; } } class Circle extends Shape{ double cx,cy; double r; Circle(int id, int type, double cx, double cy, double r){ this.id = id; this.type = type; this.cx = cx; this.cy = cy; this.r = r; } public double getArea(){ return 3.14159265 * r * r; } public double getPerimeter(){ return 3.14159265 * 2 * r; } } class Rectangle extends Shape{ double lx,ly,rx,ry; Rectangle(int id,int type,double lx,double ly,double rx,double ry){ this.id = id; this.type = type; this.lx = lx; this.ly = ly; this.rx = rx; this.ry = ry; } public double getArea(){ return (rx - lx) * (ly - ry); } public double getPerimeter(){ return 2 * (ly - ry) + 2* (lx - rx); } } class ShapeTest{ public static void main(String[] args) { Shape shapes[] = new Shape[5]; shapes[0] = new Circle(1, 1, 50.0, 50.0, 50.0); shapes[1] = new Rectangle(2, 2, -100.0, 100.0, 0.0, 0.0); shapes[2] = new Circle(3, 1, 0.0, 0.0, 10.0); shapes[3] = new Circle(4, 1, 50.0, 0.0, 30.0); shapes[4] = new Rectangle(5, 2, -75.0, 75.0, -25.0, 25.0); double sum = 0.0; for(int i=0;i<shapes.length;i++){ sum += shapes[i].getArea(); } System.out.println("合計面積は " +sum); Shape maxShape = shapes[0]; for(int i=1;i<shapes.length;i++){ if(shapes[i].getArea() > maxShape.getArea()){ maxShape = shapes[i]; } } System.out.println("一番面積の大きい図形は ID=" + maxShape.id); for(int i=0;i<shapes.length-1;i++){    for(int j=i+1;j<shapes.length;j++){     if(shapes[j].getArea() > shapes[i].getArea()){      Shape tmp = shapes[i];      shapes[i] = shapes[j];       shapes[j] = tmp;      }     }    } System.out.println("周の長さの大きい順は "); for(int i=0;i<shapes.length;i++){ System.out.print(" " +shapes[i].id); } System.out.println(); } } 合計面積は 23495.574275 一番面積の大きい図形は ID=2 周の長さの大きい順は 2 1 4 5 3 問3まではできたのですが、問4がわかりません。どうしたらいいのですか?解説とソースをお願いします。汚いソースで買得も難しいと思いますがよろしくお願いします。

    • ベストアンサー
    • Java
  • 今Javaのゲームを作るアプリケーションの「eclipse」でゲームを

    今Javaのゲームを作るアプリケーションの「eclipse」でゲームを作っているのですが、 次の文の意味がわかりません。だれか教えてください。 ----------------------------------------------------------------------------------- if ( spkey == true ) { speed = speed - 0.25 ; } else { speed = speed + 0.25 ; } if ( speed < - 6 ) speed = - 6 ; if ( speed > 6 ) speed = 6 ; cy = cy + ( int ) speed ; ------------------------------------------------------------------------------------ できるだけわかりやすくお願いします。

    • ベストアンサー
    • Java
  • javaプログラミングの質問です。

    プログラムで数値の奇遇、合計値、最大値までは出せたのですが平均値の出し方がわかりません。 どこに何を入れればいいかを教えてください。お願いします。 public class pazu{ public static void main(String[] args){ int sum =0,saidai; System.out.println("コマンドラインパラメータは"+args.length+"個です"); for(int i=0;i<args.length;i++){ int x=Integer.parseInt(args[i]); if(pazu.is_even(x)) System.out.println(args[i]+"は偶数です"); else System.out.println(args[i]+"は奇数です"); sum+=x; } saidai=pazu.max(args); System.out.println("合計:"+sum); System.out.println("最大:"+saidai); } static boolean is_even(int number){ return number%2==0; } static int max(String[] number){ int max =0; for(int i=0;i<number.length;i++){ if(max<Integer.parseInt(number[i])){ max=Integer.parseInt(number[i]); } } return max; } }

    • ベストアンサー
    • Java
  • appletを使ったプログラミング2

    前回も違う質問をさせて頂いたのですが、bubble sortとselection sortを ボタンを押す事によって順次長方形を左から右に動かしたいのですが、 どのようにしたら良いのか止まってしまいました。 import java.awt.*; import java.applet.*; import java.awt.event.*; import java.util.Random; import java.io.*; public class GUI extends Applet { public void update(Graphics g) { paint(g); } Button button = new Button("Sort Me"); Label text = new Label("Pink: Selection Sort" + "\n" + "Green: Inseration Sort"); int[] store = new int[20]; public static int[] findValue(int [] store){ int rand; for (int i = 0; i < store.length; i ++){ do{ rand = (int)(Math.random()*51)+10; } while(doesExists(rand, store, i)); store[i] = rand; } return store; } private static boolean doesExists(int rand, int[] arr, int i){ if(i != 0){ for(int j =0; j < i; j++){ if(rand == arr[j]){ return true; } } } return false; } int Counter = 0; int xScale = 0; public void displayRectangles(Graphics g) { if(Counter < 20) { xScale += 12; int x = 80 + xScale; int H = store[Counter]; g.setColor(Color.pink); g.fillRect(x, (140 - H), 10, H); g.setColor(Color.green); g.fillRect(x, 140, 10, H); Counter++; } } private void Rectangles(int xScale, int y, int W, int Counter){ this.xScale = xScale; this.Counter = Counter; } public void setxScale(int xScale){ this.xScale = xScale; } public void setCounter(int Counter){ this.Counter = Counter; } public int getxScale(){ return xScale; } public int getCounter(){ return Counter; } public void selectionSort(int[] list){ int min,temp; int n = list.length; for(int p = 0; p < n-1; p++){ min = p; for (int i = p+1; i < n; i++){ if (list[i]<list[min]) min = i; if (p != min){ temp = list[p]; list[p] = list[min]; list[min] = temp; } } } } public void insertionSort (int[] list){ int i,temp; int n = list.length; for(int k = 1;k < n; k++){ temp = list[k]; i = k; while (i > 0 && temp < list[i-1]){ list[i] = list[i -1]; i--; } list[i] = temp; } } public void init() { setSize(500, 350); setBackground(Color.WHITE); add(button); add(text); button.addActionListener(new buttonHandler()); store = findValue(store); } int c = 0; public void paint(Graphics g) { //scrambleRectangles(store); // scrambled array will be newStore[] c++; displayRectangles(g); if(c < 20) { repaint(); } } int count = 0; class buttonHandler implements ActionListener{ public void actionPerformed(ActionEvent e){ count ++; button.setLabel("pass " + count); if(e.getActionCommand()=="pass") repaint(); } } } 最終形は以下のリンクの様にしたいのですが、どのようにしたら良いのか分かりません。 http://hills.ccsf.cc.ca.us/~cconner/Java/Sorts/S … どなたかアドバイス頂けますでしょうか? よろしくお願い致します。

  • プログラミングについて

    初めまして、初めて質問させていただきます。 いきなりですが最近プログラミングにはまり、独学でCプロをやり始めた物なのですが、以下のようなプログラムをCプロで打ち込んでコンパイルしたところ int main(void) { int i,j,height,length; scanf("%d",&height); if(height % 2 ==0) { putchar("invalid"); { return 0; } } scanf("%d",&length); if(height>=1 && height<=100) for(i=1;i<=height;i++){ for(j=1;j<=length;j++) if(i ==1 || i==height) { putchar('e'); } else if( i !=height/2+1){ putchar('e'); for(j=1;j<length;j++) putchar('.'); } else{ for(j=1;j<=length/2+1;j++) putchar('e'); for(j=length/2+1;j<length;j++) putchar('.'); } putchar('\n'); } return(0); } ――――――――――― Main.c:5:1: warning: implicitly declaring library function 'scanf' with type 'int (const char *restrict, ...)' [-Wimplicit-function-declaration] scanf("%d",&height); ^ Main.c:5:1: note: include the header <stdio.h> or explicitly provide a declaration for 'scanf' Main.c:8:5: warning: implicit declaration of function 'putchar' is invalid in C99 [-Wimplicit-function-declaration] putchar("invalid"); ^ 2 warnings generated. ――――――――― と上のようなエラー内容が出てしまい、丸一日自分で模索しても何故こんなエラーが出て来てしまうのか分からない為、教えて頂けるとありがたいです。

  • javaのlengthに対して質問です

    javaのlengthに対して質問です public class Gauss { public static void main(String[] args){ int[] ia = new int[101]; for (int i = 0 ; i<ia.length;i++); } } 今の場合 ia.lengthはどれくらいの長さですか? ia だから2?

    • ベストアンサー
    • Java
  • Javaの判定処理について

    いつもお世話になっております。 以下のJavaのコードで、 ☆の部分のif文が何を実施しているのか 分からないのですが、 ご存知の方がいられましたら ご教授をいただけますでしょうか。 【コード】 String value = "あいうえお12345"; int length = value.length(); for (int i = 0; i < length; i++) { char c = value.charAt(i); if ((c & ~0x7f) != 0) { ←☆ 処理1 } }

    • ベストアンサー
    • Java
  • パックマンゲーム 敵の動きについて c言語

    現在 簡単なパックマンゲームを作成しています。 以下のプログラムだと、 ・敵が壁にあたると全く動かなくなる という問題点があります。なので、例えば、「動ける(壁でない)方向のうち、移動後の距離が一番自機に近い方向に移動する」というアルゴリズムにしようと思ったのですが、全くコードが思い浮かべません。 どんなコードをかけばいいでしょうか。 できればコードを具体的に教えていただきたいです。 お願いいたします。 #pragma warning(disable:4996) #include <stdio.h> #include <stdlib.h> // system() #include <windows.h> // Sleep() #include <conio.h> // kbhit() #define SIZE 15 // 一辺の長さ #define EMPTY 0 // 何もない場所は0 #define WALL 1 // 壁 #define FOOD 2 // えさ(ドット) #define POWER_FOOD 4 //パワーエサ #define PACMAN 3// 主人公 #define SIZE 15 // 一辺の長さ #define EMPTY 0 // 何もない場所は0 #define WALL 1  // 壁 #define FOOD 2  // えさ(ドット) #define PACMAN 3// 主人公   int main() {     int wait_time = 300;     int x, y;     int food_count = 0;//エサの個数     int cx, cy;//パックマン     int ex, ey;//敵     int key;     int point = 0;     int kx, ky;//自機の座標を保存     int mx, my;//敵機の座標を保存     int life = 3;     // 壁 1、えさ 2、空白 0     // field[y][x]となる     int field[SIZE][SIZE] =     {         { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },       ・・・・・・・・・         { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },     };               cx = 7, cy = 7; //パックマンの初期位置     ex = 1, ey = 1;//敵の初期位置     //エサの個数を数える     for (y = 0; y < SIZE; y++)     {         for (x = 0; x < SIZE; x++)         {             if (field[y][x] == FOOD)                 food_count++;         }       }       while (1)     {         // まずはPACMANの動き           if (kbhit())         {             key = getch();//             kx = cx, ky = cy;//自機の座標を保存する             switch (key)             {             case '4':                 cx--;                 break;             case '6':                 cx++;                 break;             case '2':                 cy++;                 break;             case '8':                 cy--;                 break;             }               cx = (cx + SIZE) % SIZE; //ワープ                        switch (field[cy][cx])             {             case WALL://壁                 cx = kx, cy = ky; //動けないので cx, cy を元に戻す                                                   break;             case FOOD:                 field[cy][cx] = 0;//餌を食べると餌が消える                 point += 10;      //得点加算                 food_count--;     //餌の数を減らしていく                 break;             }             if (cx == ex && cy == ey)                 life--;         } // 敵機の動き         mx = ex; my = ey;//敵機の座標を保存する           if (ex > cx)             ex--;         else if (ex < cx)             ex++;         else if (ey > cy)             ey--;         else if (ey < cy)             ey++;                 if (field[ey][ex] == WALL)  //もし敵機が壁に当たったらもとの座標に戻す。         {             ex = mx; ey = my;         }         ex = (ex + SIZE) % SIZE; //ワープ           if (cx == ex && cy == ey) {             life--;             ex = 7, ey = 7;             Sleep(wait_time);         } //画面表示 for (y = 0; y < SIZE; y++) {             for (x = 0; x < SIZE; x++) {                 if (x == cx && y == cy)                     printf("C ");                 else if (x == ex && y == ey)                     printf("◇");                 else if (field[y][x] == FOOD)                     printf(". ");                 else if (field[y][x] == WALL)                     printf("■");                 else                     printf(" ");             }             printf("\n");         }         printf("\n");         printf(" (cx,cy) = (%d, %d)\n", cx, cy);         printf(" point: %d\n", point);         printf("  LIFE: %d\n", life);           if (life == 0)         {             printf("game over\n");                       Sleep(wait_time);             break;         }           if (food_count == 0)             {             printf("game crer\n");             Sleep(wait_time);             break;           }         Sleep(wait_time);//