-
Giúp mình lỗi này trong java với
Mình mới học java bạn nào giúp mình với. Mình làm một bài test này khi chạy thì nó báo lỗi "non-static variable this cannot be referenced from a static context" mà ko tim thấy lỗi ở đâu.
import java.lang.*;
import java.io.*;
class test1
{
class Moto
{
String make;
String color;
boolean engin;
void startEngine(){
if (engin)
System.out.println("the engine is already");
else {
engin= true;
System.out.println("the engine is now on");
}
}
void showAtts() {
System.out.println("this motocycle is a" + color + "" + make + ".");
if (engin)
System.out.println("the engine is on.");
else System.out.println("the engine is off.");
}
}
public static void main(String args[]){
Moto m = new Moto();
m.make = "Yamaha";
m.color = "yellow";
System.out.println("calling showatt...");
m.showAtts();
System.out.println("starting engine...");
m.startEngine();
System.out.println("calling showatt...");
m.showAtts();
System.out.println("starting engine...");
m.startEngine();}
};
-
Bạn thử so sánh thử xem đoạn code của mình với của bạn khác nhau chỗ nào nha(đừng nới là kiểu chữ với màu chữ nha/[IMG]data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAA l21bKAAAAA1BMVEXh5PJm+yKVAAAAAXRSTlMAQObYZgAAAApJR EFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII=[/IMG]):
package test;
import java.lang.*;
import java.io.*;
class test1
{
static class Moto
{
String make;
String color;
boolean engin;
void startEngine(){
if (engin)
System.out.println("the engine is already");
else {
engin= true;
System.out.println("the engine is now on");
}
}
void showAtts() {
System.out.println("this motocycle is a" + color + "" + make + ".");
if (engin)
System.out.println("the engine is on.");
else System.out.println("the engine is off.");
}
}
public static void main(String args[]){
Moto m = new Moto();
m.make = "Yamaha";
m.color = "yellow";
System.out.println("calling showatt...");
m.showAtts();
System.out.println("starting engine...");
m.startEngine();
System.out.println("calling showatt...");
m.showAtts();
System.out.println("starting engine...");
m.startEngine();}
};
-
static class Moto
Nên xem lại cách dùng static
Quyền viết bài
- Bạn Không thể gửi Chủ đề mới
- Bạn Không thể Gửi trả lời
- Bạn Không thể Gửi file đính kèm
- Bạn Không thể Sửa bài viết của mình
-
Nội quy - Quy định
Theo điều tra tình hình sử dụng thuốc lá ở người trưởng thành năm 2020 do Bộ Y tế triển khai, ngày nay tỷ lệ đàn ông Việt Nam hút thuốc đang ở mức 42,3%. Không chỉ có khả năng gây ung thư và một...
Những thói quen khiến "cuộc vui" của hai người trở nên... dở dang