Java 使用其他类中的变量
我有两节课。我想在第二节课(课堂问题)中使用第一节课的Java 使用其他类中的变量,java,class,variables,Java,Class,Variables,我有两节课。我想在第二节课(课堂问题)中使用第一节课的英语和法语。请帮助我修复该代码,因为它显示了一个错误 代码块: package josephtraduire; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; public class FileParser implements IParser{
英语
和法语
。请帮助我修复该代码,因为它显示了一个错误
代码块:
package josephtraduire;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class FileParser implements IParser{
@Override
public void parseFile () throws IOException{
String french="";
String english="";
try( BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\User\\Desktop\\text.txt"))){
String line;
while ((line = br.readLine())!=null){
String[] pair = line.split(";");
french=(pair[0]);
english=(pair[1]);
}
}
}
}
及
packagejosephtraduire;
导入java.io.BufferedReader;
导入java.io.BufferedWriter;
导入java.io.FileWriter;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.util.Date;
导入java.util.Scanner;
公共类问题扩展FileParser{
弦运动;
字符串响应;
字符串名;
int nb;
国际nbquest;
弦乐演奏;
问题(字符串名称,int-nb){
this.name=name;
这个.nb=nb;
}
问题()引发异常{
InputStreamReader isr=新的InputStreamReader(System.in);
BufferedReader in=新的BufferedReader(isr);
{
系统输出打印号(“入口votre nom”);
字符串nom=in.readLine();
名称=名称;
}
做{
System.out.println(“问题名称:(最多20个)”;
扫描器nombrequest=新扫描器(System.in);
nbquest=nombrequest.nextInt();
nb=nbquest;
}而(nbquest>20 | | nbquest我将忽略源代码并用一般术语回答这个问题。您可以直接或使用getter访问对象的实例变量。在本例中我将使用getter
实例变量-只要类存在,它们就存在
在类的主体内声明,但不在任何方法或
建造师
方法变量-通常与方法的寿命相同,不再有效
做一些工作,然后离开,他们在一个小时内宣布
方法/构造函数
请格式化您的代码并只发布相关的代码片段…您的…您的编码格式…恐怖…请再次格式化您的代码并删除所有不必要的空白,等等…首先使用以下方法格式化您的代码:ctrl+a和ctrl+shift+f(如果您使用eclipse…)你真的需要拿起一本初学者的书,或者读一本教程。不要着急。
package josephtraduire;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.Scanner;
public class Question extends FileParser {
String mot ;
String reponse ;
String name;
int nb;
int nbquest ;
String traduction;
Question (String name , int nb){
this.name=name;
this.nb=nb;
}
Question() throws IOException{
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(isr);
{
System.out.println("Entrer votre nom ");
String nom = in.readLine();
name=nom;
}
do {
System.out.println("Rentrez le nombre de question : ( max 20 )");
Scanner nombrequest = new Scanner(System.in);
nbquest = nombrequest.nextInt();
nb=nbquest;
} while ( nbquest>20 ||nbquest<=0);
}
public void Play () throws IOException{
int i=0;
boolean bool=true;
int score=0;
String continuer;
Date maDate = new Date();
String a = maDate.toString() ;
while (i<nbquest && bool) {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(isr);
System.out.println("donner la traduction de "+french);
String reponseee = in.readLine();
traduction=reponseee;
if(bool=true ){
if(traduction.equals(english)){
score++;
System.out.println("Bravo! Bonne reponse");
}else{
System.out.println("Mauvaise reponse");
}
}
public class ClassThatWantsFields {
public String combineFields(ClassWithFields classWithFields){
//if I have access to the object classWithFields then I have access to its
//public methods (and possibly also protected and default access; but this is outside the scope of this question)
return classWithFields.getEnglish()+classWithFields.getFrench();
}
public static void main(String[] args){
ClassWithFields classWithFields=new ClassWithFields();
ClassThatWantsFields classThatWantsFields=new ClassThatWantsFields();
System.out.println(classThatWantsFields.combineFields(classWithFields));
}
}
public class ClassWithFields {
private String English; //these are instance variables, they live for as long as the object lives
private String French;
private String preservedMayFly;
public ClassWithFields(){
English="A language called English";
French="A language called French";
//mayfly is a method variable, it will be gone once the constructor
//exits, anything you want to keep for the life of the object should
//NOT be a method variable
String mayfly="I won't live long";
//preservedMayFly is an instance variable and will live as long as
//the object
preservedMayFly=mayfly+"but I can be used within the method"
}
public String getEnglish() {
return English;
}
public String getFrench() {
return French;
}
}