我想从文本文件中读取一些数据.这是文本文件中数据的格式:
A,20, ,0
B,30, ,0
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
public ArrayList rechercherSalle()
{
String nom;
String ligne;
ArrayList<Salle> listeSalles = new ArrayList<Salle>();
Salle salle = new Salle();
try {
InputStream flux = new FileInputStream("salle.txt");
InputStreamReader lecture = new InputStreamReader(flux);
BufferedReader buff = new BufferedReader(lecture);
ligne = buff.readLine();
while (ligne != null) {
String[] objetSalle = ligne.split(",");
nom = objetSalle[0];
String capacite_maxString = objetSalle[1];
Integer capacite_max = Integer.parseInt(capacite_maxString);
String capacite_actuelleString = objetSalle[3];
Integer capacite_actuelle = Integer.parseInt(capacite_actuelleString);
String proprietaire = objetSalle[2];
salle = new Salle(); …Run Code Online (Sandbox Code Playgroud) 我有一个任务:
修改类汽车,使其覆盖有它自己版本的方法setCapacity其输出的消息"无法改变汽车的能力",不改变发动机的能力.
我试图解决下面代码的任务,但它继续使用Vehicle类的setCapacity方法而不是Car方法.
class Vehicle // base class
{
int capacity;
String make;
Vehicle(int theCapacity, String theMake)
{
capacity = theCapacity;
make = theMake;
}
void print()
{
System.out.println("Vehicle Info:");
System.out.println(" capacity = " + capacity + "cc" );
System.out.println(" make = " + make );
}
public void setCapacity(int newCapacity)
{
capacity = newCapacity;
System.out.println("New capacity = " + capacity);
}
}
class Car extends Vehicle
{
String type, model;
Car(int theCapacity, String …Run Code Online (Sandbox Code Playgroud) class Example{
public static void main(String args[]){
int x=99;
if(x++==x){
System.out.println("x++==x : "+x); //Why this code line is not run?
}
if(++x==x ){
System.out.println("++x==x : "+x);
}
}
}
Run Code Online (Sandbox Code Playgroud)
为什么第一条语句没有println执行?
关键字"this"在TextView中的含义是 什么textView = new TextView(this);
下面是引用片段
public class DisplayMessageActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_display_message);
Intent intent = getIntent();
String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
setContentView(textView);
}
....
Run Code Online (Sandbox Code Playgroud)
我的猜测是初始化对象textView但是用什么?
我正在尝试定义一个用户定义的元素数量(度)的数组,然后是一个允许用户一次设置一个元素(系数)的方法.
class Polynomial
{
public Polynomial(int degree)
{
double[] coef = new double[degree];
}
public double[] setCoefficient(int index, double value)
{
coef[index] = value; //coef was set in the constructor
return coef;
}
}
Run Code Online (Sandbox Code Playgroud)
我收到了编译错误coef[index] = value;.
给定2D矩阵,任务是找到一个沙漏的最大和。
沙漏杯由以下形式的7个电池组成。
Run Code Online (Sandbox Code Playgroud)A B C D E F G
我的实施出现问题,我得到了奇怪的答案。我应该得到19,但返回36。
这是我的代码:
static int hourglassSums(int[][] arr)
{
return IntStream.range(0, 4).map(x -> {
return IntStream.range(0, 4).map(y ->
arr[y][x] + arr[y][x + 1] + arr[y][x + 2]
+ arr[y + 1][x + 1]
+ arr[y + 2][x] + arr[y + 2][x + 1] + arr[y + 2][x + 2]
).sum();
}).max().getAsInt();
}
public static void main(String[] args)
{
System.out.println("Max sum :: " + hourglassSums(new int[][] {
{1, 1, …Run Code Online (Sandbox Code Playgroud) 我刚刚开始阅读Java 8概念,而我却发现一些不足以说服我的东西是Interface中缺少compose和identity方法BiFunction。就我从Java 8文档中读取的内容来看,我Bifunction似乎与Functioninterface 相同,期望它需要一个附加参数,因此在这种情况下Bifunction应该具有与Functioninterface相同的所有功能,但事实并非如此。
因此,有人可以帮助我找到省略这些方法的原因吗?
我正在使用while循环,它应该在它应该时终止.如果它正常工作,那么当randno它是== highbound或时它会终止== lowbound.
循环代码:
do {
do {
randno = (int) (Math.round((Math.random()*(4)) + 0.5)-1);
direction = getDirection(randno,heading);
} while (robot.look(direction)==IRobot.WALL);
System.out.println(randno);
System.out.println(highbound);
System.out.println(lowbound);
System.out.println("---------------");
} while (randno!=lowbound | randno!=highbound);
Run Code Online (Sandbox Code Playgroud)
输出是3 3 2 ------,或者2 3 2 ------,因此循环应该结束.第一个循环正确结束(我嵌入它们试图让它工作......).出了什么问题?
有人可以帮我找到这个代码的大O吗?我已经尝试过计算它,但我只是不明白怎么做.
int n = //user input
for (int i = 0; i < n; i++){
for (int j = 1; j < n; j = j * 2){
System.out.println(i * j);
}
}
Run Code Online (Sandbox Code Playgroud) 对于以下方法:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
if(v.getId() != R.id.listViewToDo) {
return;
}
menu.setHeaderTitle("What will you like to do?");
String[] options = ( "Delete Task", "Return" );
for(String option : options) {
menu.add(option);
}
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
赋值的左侧必须是变量.
我该如何解决?
java ×10
android ×2
arrays ×2
java-8 ×2
methods ×2
big-o ×1
buffer ×1
constructor ×1
do-while ×1
inputstream ×1
nested-loops ×1
outputstream ×1
overriding ×1
sorting ×1