I am following the numerous guides from this website: https://www.w3schools.com/kotlin/kotlin_arrays.php
I am experimenting along the way with my own ideas and such.
For the section detailing how to access elements in an array, I decided that I want to create an output that uses correct grammar in English. I want the list to be displayed as such:
"Volvo, BMW, Ford and Audi."
How can I achieve this? I`m of course an absolute beginner.
Thank you!
fun main()
{
val cars = arrayOf("Volvo", "BMW", "Ford", "Mazda")
println(cars[3]) // Prints "Mazda"
if ("Mazda" in cars)
{
println("It exists!")
}
else
{
println("It doesn`t exist!")
}
cars[3] = "Audi"
println(cars[3])
if ("Mazda" in cars)
{
println("It exists!")
}
else
{
println("It doesn`t exist!")
}
println("There are " + cars.size + " elements in this array:")
for (w in cars) // The dilemma begins from here
while (w < cars.size - 1)
{
print(w + ", ")
}
else
{
print(w + "and " + cars[3] + ".")
}
}
#Tenfour04
Thank you for your post.
Here is what I settled at before your last post
for ((i, w) in cars.withIndex())
{
if (i < cars.size - 1)
{
print(w + ", ")
}
else
{
for (i in 0..cars.size-4)
print("and " + cars[3])
}
}
which gave me the output of
Volvo, BMW, Ford, and Audi
After I`ve corrected it with your solution, thank you very much.
The output is still the same:
Volvo, BMW, Ford, and Audi
Now I am wondering how I can remove the third comma. The one after "Ford", so that the final output would be:
Volvo, BMW, Ford and Audi
while is a loop with a condition (in the parentheses after it). That's not what you want. And in your comment, you replaced for with println, which doesn't make sense.
And your else branch makes the mistake of using both w and cars[3] in you final output, two different ways of getting the name of the last car, so it appears twice in your output.
Since you are using both the elements of the array and their indices, you should iterate both at once using withIndex, like this.
Here's how you can iterate both the indices and the cars so you can do an if condition on the index (position) in the list of each item.
for ((i, w) in cars.withIndex()) {
if (i < cars.size - 1)
{
print(w + ", ")
}
else
{
print("and " + w + ".")
}
}
But it's cleaner to use string templates like:
for ((i, w) in cars.withIndex()) {
if (i < cars.size - 1)
{
print("$w, ")
}
else
{
print("and $w.")
}
}
When you have short single function calls in each branch of if/else, it sometimes is cleaner looking code to use when instead:
for ((i, w) in cars.withIndex()) {
when {
i < cars.size - 1 -> print("$w, ")
else -> print("and $w.")
}
}
Or for a completely different strategy, you could iterate all but the last item by dropping it, and then use just the last item:
for (car in cars.dropLast()) {
println("$car, ")
}
print("and ${cars.last()}")
For your comment in your own answer, you can use a when statement with three conditions:
for ((i, w) in cars.withIndex()) {
when {
i < cars.size - 2 -> print("$w, ")
i == cars.size - 2 -> print("$w ")
else -> print("and $w.")
}
}
Related
How to keep the smoke set changing while the rocket goes up?
I don't understood very well, but the set only changes while the rocket is at the base.And he wasn't supposed to stand still.
veja o movimento do vídeo no link ->
https://i.imgur.com/RRvFqBR.mp4
The loop for(int h = 0; h < 29; h++){ maintains the set by changing the condition of the increment, and only takes off after that. Then the set stops changing.
#include <unistd.h>
#include <stdio.h>
#define LINE 11
#define COLN 12
#define R_COLN 12
#define R_LINE 9
#define R_SET 2
#define DELAY 95000
//string to display the rocket
const char rocket[LINE][COLN+1]={
" ^ ",
" /^\\ ",
" |-| ",
" |R| ",
" |O| ",
" |C| ",
" |K| ",
" |E| ",
" /|T|\\ ",
" / | | \\ ",
" | | | | "
};
const char smoke[R_SET][R_LINE][R_COLN+1]={
{
" ' * ",
" * + . ' ",
" - . + ",
" . ' : . ",
" + ' ' * . ",
" . * . ",
" . ' : ",
" . ' . ",
" ' "
},
{
" * ' ",
" ' . + * ",
" + . - ",
" . : ' . ",
" . * ' ' + ",
" . * . ",
" : ' . ",
" . ' . ",
" ' "}
};
int main(){
int jumpControlAtBottom = 0;
int shifControl = 0;
int smoke_set = 0;
for(int h = 0; h < 29; h++){ //frame
for (jumpControlAtBottom = 0; jumpControlAtBottom < 28; ++jumpControlAtBottom){
// Jump to bottom of console
printf("\n");
}
for(int i = 0; i< LINE; i++){
printf("%.*s\n", COLN, rocket[i]);
}
for(int y=0; y<R_LINE; ++y){
printf("%.*s\n", R_COLN, smoke[smoke_set][y]);
}
smoke_set=(smoke_set+1)%R_SET; // Advance to the next set
// (or go back to the first one).
fflush(stdout); // Draw the current frame on the screen.
usleep(DELAY); // Pause to be visible.
}
for (shifControl = 0; shifControl < 28; ++shifControl){
// Rocket move on the basis of delay
usleep(DELAY);
// move rocket a line upward
printf("\n");
}
return 0;
}
Currently your logic is:
Draw one frame.
Change smoke set.
Repeat 1-2 for 29 frames.
Draw line to push frame up.
Repeat 4 to keep pushing frames up.
From that it is obvious the smoke will stop changing at step 4. So the logic needs to include the take off elevation in step 1. The easiest way to do that is to put the draw frame into a function and add the elevation as a parameter.
Here is an example:
void draw_frame(int elevation)
{
int jumpControlAtBottom = 0;
static int smoke_set = 0;
for (jumpControlAtBottom = 0; jumpControlAtBottom < 28 + elevation; ++jumpControlAtBottom){
// Jump to bottom of console
printf("\n");
}
for(int i = 0; i< LINE; i++){
printf("%.*s\n", COLN, rocket[i]);
}
for(int y=0; y<R_LINE; ++y){
printf("%.*s\n", R_COLN, smoke[smoke_set][y]);
}
smoke_set=(smoke_set+1)%R_SET; // Advance to the next set
// (or go back to the first one).
// Push image up by elevation
for (int ix = 0; ix < elevation; ix++) {
printf("\n");
}
fflush(stdout); // Draw the current frame on the screen.
usleep(DELAY); // Pause to be visible.
}
int main(){
int shifControl = 0;
// No elevation - engine starting up
for(int h = 0; h < 29; h++){ //frame
draw_frame(0);
}
// take off has occured
for (shifControl = 0; shifControl < 28; ++shifControl){
// Rocket move on the basis of delay
// move rocket a line upward
draw_frame(shifControl);
}
return 0;
}
It seems to me like you're struggling to understand the procedural nature of C, which probably means you're guessing and playing around with code from other (probably poor) examples.
This is akin to guessing how to prepare a chicken for cooking and then asking your guests how to prepare a chicken for cooking, after the chicken is cooked. You have an application that does most of what you want. How did you come up with this code, yet not know how to apply such a simple tweak?
If you don't understand the procedural nature of C, it stands to reason that any exercise that has you read the procedural nature of C in order to extract some logic and repeat it is terrible for you. Where is your book? Start with the basics.
To be clear, this answer may not seem relevant, but it does actually answer your question: you need to hoist some of your smoke-related logic out into a different function so that you can reuse it later... but before you do that, you need to be able to read C, and we're not here to do your work for you, rather to guide you in the right direction.
I am new to Kotlin and I am trying to write a function to convert a number to Roman Numerals; however, I am unable to set the values of "numberBeingChecked" and "romanNumeral" from inside the loop, would you know why?
fun encode(num: Int): String {
var numberBeingChecked: Int = num
var romanNumeral: String = ""
myLoop# while (numberBeingChecked > 0) {
when (numberBeingChecked) {
0 -> {
return romanNumeral
}
in 1..4 -> {
romanNumeral + "I"
numberBeingChecked - 1
break#myLoop
}
in 5..9 -> {
romanNumeral + "V"
numberBeingChecked - 5
break#myLoop
}
in 10..49 -> {
romanNumeral + "X"
numberBeingChecked - 10
break#myLoop
}
in 50..99 -> {
romanNumeral + "L"
numberBeingChecked - 50
break#myLoop
}
in 100..499 -> {
romanNumeral + "C"
numberBeingChecked - 100
break#myLoop
}
in 500..999 -> {
romanNumeral + "D"
numberBeingChecked - 500
break#myLoop
}
else -> {
romanNumeral + "M"
numberBeingChecked - 1000
break#myLoop
}
}
}
return romanNumeral
}
fun main(args: Array<String>) {
encode(0)
encode(1)
encode(21)
encode(2008)
encode(1666)
}
Please let me know if I am doing something wrong, your help will be appreciated.
Kind regards
You don't really reassign these variables in your code. numberBeingChecked - 5 expression does not decrease the value of numberBeingChecked. It just subtracts 5 from numberBeingChecked and does nothing with the result. To decrease numberBeingChecked by 5 you need to use -= operator: numberBeingChecked -= 5. Same applies to romanNumeral, e.g.: romanNumeral += "V".
Another bug in your code is that you always break from the loop after the first step. You should only get out of the loop when numberBeingChecked is zero, so you really need to remove all these break instructions.
Additionally, you should avoid creating strings by concatenating multiple times, because it results in copying the whole string with each step. Instead, create a StringBuilder, append to it and at the last step invoke toString() on it to get a result.
The resulting code looks like this:
var numberBeingChecked: Int = num
var romanNumeral = StringBuilder()
while (numberBeingChecked > 0) {
when (numberBeingChecked) {
in 1..4 -> {
romanNumeral.append("I")
numberBeingChecked -= 1
}
...
}
}
return romanNumeral.toString()
We can simplify it even further with buildString() utility which helps us create the StringBuilder and convert it to a string:
var numberBeingChecked: Int = num
return buildString {
while (numberBeingChecked > 0) {
when (numberBeingChecked) {
in 1..4 -> {
append("I")
numberBeingChecked -= 1
}
...
}
}
}
However, the latter uses some advanced features of Kotlin, it is harder to explain/understand how it works internally and therefore it may not be the best for learning purposes.
You're not performing an assignment. You need either an equals sign as i=i+1, or a shorthand like i++.
Although I've seen similar question's nothing quite answers why this doesn't work, and I'm unaware of an alternative.
I'm making a very simple calculator and when pulling the expression from the string I need to replace symbols such as '×' and '÷' with operators recognized by the eval.
Currently I'm trying to work through the string one character at a time and copy it into a new string, replacing where necessary. It seems that none of the if statements checking for characters in the string are ever called and I dont know why.
for (var i = 0; i < (expressionPre.length) ; i++) {
alert(expressionPre[i]);
if (expressionPre[i] == "÷") {
expressionPost += "/";
} else if (expressionPre[i] === '×') {
expressionPost += "*";
alert("Finally!");
} else if (expressionPre[i] == "−") {
expressionPost += "-";
} else if (expressionPre[i] % 1 == 0) {
expressionPost += expressionPre[i];
}
alert(expressionPost[i]);
}
as #beaver say, you should use the replace function directly.
this is a function who replace all occurence of text with another one
function tools_replaceAll(str, find, replace) {
return str.replace(new RegExp(find, 'g'), replace);
}
var str = "(1 ÷ 2 ÷ 2) × 3 × 3 − 4− 4 + 5 + 5";
str = tools_replaceAll(str, "÷" , "/" ) ;
str = tools_replaceAll(str, "×" , "*" ) ;
str = tools_replaceAll(str, "-" , "-" ) ;
alert( str ) ;
So i am fairly new to programming but I have the basics like loops and arrays down and now I'm moving on the classes and objects.
I want my to get my pet to "eat" and output 3 food elements using an array.
This is my main method
Food[] f = new Food[3];
for(int i = 0; i < 3; i++){
f[i] = new Food();
System.out.println("Your " + pet.type + " ate some " + food.name);
pet.eat(food);
}
And I am calling the food from the "Food Class"
public class Food {
int nutrition; // scale from 0 - 5 where 5 is super nutritious
String name; // name of food
public Food(){
Random r = new Random();
int num = r.nextInt(6);
if (num == 0){
name = "rocks";
nutrition = 0;
} else if (num == 1){
name = "seeds";
nutrition = 1;
} else if (num == 2){
name = "chips";
nutrition = 2;
} else if (num == 3){
name = "carrots";
nutrition = 3;
} else if (num == 4){
name = "fish";
nutrition = 4;
} else if (num == 5){
name = "steak";
nutrition = 5;
}
}
My problem here is when I try to run the loop above, it outputs 3 of the same foods even though i specified in the food class that I wanted it to generate a random one.
Example Output
Your dog ate some chips
This food is not nutritious at all...
Your dog ate some chips
This food is not nutritious at all...
Your dog ate some chips
This food is not nutritious at all...
Thanks in advance!
Your new Food object is stored in f[i].
f[i] = new Food();
But your output is coming from food.name.
System.out.println("Your " + pet.type + " ate some " + food.name);
You need to be printing f[i].name.
So for my assignment I have to take the inputs of length and width and print out patterns of "*" based on the inputs. The minimum height is 7 and only goes up by odd integers and width is any multiple of 6.
The basic format of the output using a height of 7 and width of 12:
************
************
*** ***
*** ***
*** ***
************
************
So basically the first and last 2 lines are straight through the entire width, with the odd numbered rows containing 3 asterisks followed by 3 spaces, until it reaches the end of the width. The even numbered rows start off with 3 spaces.
I've figured out how to print the first two lines using the following code:
do
{
printf("*");
++i;
}while(i<width);
printf("\n");
do
{
printf("*");
++j;
}while(j<=width);
printf("\n");
But for the life of me, I cannot come up with the correct way to use basic nested loops to print out the inside pattern. I asked a programmer friend who is unfamiliar with C but wrote up a basic program in Java. I don't know Java and have tried to translate it but notice some big discrepancies in the logic between the two languages that is causing me headaches. Here is his code:
// LOGGING
var consoleLine = "<p class=\"console-line\"></p>";
console = {
log: function (text) {
$("#console-log").append($(consoleLine).html(text));
}
};
// PATTERN PARAMETERS
var rows = 6;
var cols = 7;
// hard code a space so html respects it
var space = " "
console.log("cols: " + cols + " rows: " + rows);
for (y = 0; y < rows; ++y) {
var line = "";
for (x = 0; x < cols; ++x) {
// First two and last two rows do not have patterns and just print filled
if (y == 0 || y == 1 || y == rows - 1 || y == rows - 2) {
line += "*";
} else {
if (y % 2 == 0) {
// Even row
line += x % 6 < 3 ? "*" : space;
} else {
// Odd row
line += x % 6 >= 3 ? "*" : space;
}
}
}
console.log(line);
}
Please help me or point me in the right direction!! I've searched online but can't seem to find a solution that's worked yet!
Edit- forgot to mention that all "printf" uses can only print one character at a time... Such as a single *
Edit edit- I GOT IT WORKING!!!! Thank you all so, so much for your input and guidance! Here's what I have that is working perfectly:
for (y = 0; y < height; ++y)
{
printf("\n");
for (x = 0; x < width; ++x)
{
// First two and last two rows do not have patterns and just print filled lines
if (y == 0 || y == 1 || y == height - 1 || y == height - 2)
{
printf("*");
}
else
{
if (y % 2 == 0)
{
if(x%6<3)
{
printf("*");
}
else
{
printf(" ");
}
} else {
// Odd row
if(x%6>=3)
{
printf("*");
}
else
{
printf(" ");
}
}
}
}
printf("\n");
Write a function with 3 arguments n,a,b that prints n groups of 3 of each argument a and b alternately. You can call this function to print the 4 different kinds of lines. You can make a loop to print the middle section repeatedly. Have fun!
A simpler alternative:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
int row, col, height = atoi(argv[1]), cols = atoi(argv[2]);
for (row = 0; row < height; row++) {
for (col = 0; col < cols; col++) {
putchar(row < 2 || row >= height - 2 ||
col % 6 / 3 == row % 2 ? '*' : ' ');
}
putchar('\n');
}
}