#include <stdio.h>
int main(int argc, char **argv) {
int x[5][5] = {{0,1,2,3,4},{5,6,7,8,9}};
for (int i = 0; i < 4; i++) {
for (int j = 0; i < 4; i++) {
printf("%d\n", x[i][j]); } }
return 0; }
Run Code Online (Sandbox Code Playgroud)
这本来应该给我:0 1 2 3 4 5 6 7 8 9
结果是:0 5 0 0
然后,当我将它们改为chars时:
#include <stdio.h>
int main(int argc, char **argv) {
char x[5][5] = {{'0','1','2','3','4'},{'5','6','7','8','9'}};
for (int i = 0; i < 4; i++) {
for (int j = …Run Code Online (Sandbox Code Playgroud) if (!(portField.getText().equals(""))) {
String p = portField.getText();
CharSequence numbers = "0123456789";
if (p.contains(numbers)) {
listener = new ServerSocket(Integer.parseInt(p));
while (true) {
Socket socket = listener.accept();
try {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println("Hi there, human.");
} finally {
socket.close();
}
}} else {
JOptionPane.showMessageDialog(null, "Only numbers are allowed.");
}
} else {
JOptionPane.showMessageDialog(null, "Please input a port.");
}
Run Code Online (Sandbox Code Playgroud)
问题是:JOptionPane弹出"只允许数字",即使我把数字放入portField.在CharSequence和我测试过它,只允许数字来输入,因为据我所知的方式,是正确的,和Java忽略了整个街区,并跳转到该else条款.
为什么会这样?我不应该使用else和使用else if吗?
这是run()方法:
public void run() {
ServerSocket listener = null;
if (running == true) {
btnRun.setEnabled(false);
try {
if (!(portField.getText().equals(""))) {
String p = portField.getText();
if (p.matches("[0-9]*")) {
listener = new ServerSocket(Integer.parseInt(p));
while (true) {
Socket socket = listener.accept();
try {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
out.println("Hi there, human.");
} finally {
socket.close();
}
}
} else {
JOptionPane.showMessageDialog(null, "Only numbers are allowed.");
btnRun.setEnabled(true);
running =
}
} else {
JOptionPane.showMessageDialog(null, "Please input a port.");
btnRun.setEnabled(true);
running = false;
} …Run Code Online (Sandbox Code Playgroud) 我已经阅读了这篇好文章 作为参考,但我仍然无法解决我的循环问题:
import pygame
import python
import background
import player
import parser
class Game():
map1 = parser.Parser("map1")
map1.parse()
Run Code Online (Sandbox Code Playgroud)
parser.py模块:
import os, sys
def replaceExtension(mapPath):
# content
class Parser():
def __init__(self, map, path="Maps/"):
replaceExtension(path)
# content
Run Code Online (Sandbox Code Playgroud)
当我运行我的主文件时:
map1 = parser.Parser("map1")
AttributeError: 'module' object has no attribute 'Parser'
Run Code Online (Sandbox Code Playgroud)
由于某些晦涩的原因,它只是找不到我的Parser类.
我有以下类和typedef:
class Object
{
protected:
long int id;
public:
Object(void);
~Object(void) {};
long int get_id(void);
};
typedef map<string, Object> obj_map;
Run Code Online (Sandbox Code Playgroud)
然后我有了它的孩子:
class Image: public Object
{
private:
path full_path;
int x;
int y;
img image;
public:
Image(path p, int x_pos = 0, int y_pos = 0);
~Image(void) {};
void draw(int flags = 0);
void move_north(float velocity);
void move_south(float velocity);
void move_west(float velocity);
void move_east(float velocity);
path get_path(void);
img get_image(void);
int get_x(void);
int get_y(void);
void set_path(path p);
void set_image(img _image);
void …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现一个消除字符串空格的函数。如果谓词仅显式过滤空格,我不明白为什么此列表推导式会消除第一个字符。
import Data.List
import System.IO
noSpace :: String -> String
noSpace (x:xs) = [x | x <- xs, x /= ' ']
main = do
print(noSpace("8 j 8 mBliB8g imjB8B8 jl B"))
Run Code Online (Sandbox Code Playgroud)
结果:
"j8mBliB8gimjB8B8jlB"
Run Code Online (Sandbox Code Playgroud)
应该:
8j8mBliB8gimjB8B8jlB
Run Code Online (Sandbox Code Playgroud) java ×2
boolean ×1
c ×1
c++ ×1
constructor ×1
haskell ×1
import ×1
inheritance ×1
module ×1
python ×1
runnable ×1
subclass ×1
superclass ×1
while-loop ×1